MCPcopy Create free account
hub / github.com/ZhuYanzhen1/miniFOC / fractionToString

Method fractionToString

tools/qcustomplot.cpp:7580–7625  ·  view source on GitHub ↗

! \internal Takes the fraction given by \a numerator and \a denominator and returns a string representation. The result depends on the configured fraction style (\ref setFractionStyle). This method is used to format the numerical/fractional part when generating tick labels. It simplifies the passed fraction to an irreducible form using \ref simplifyFraction and factors out any inte

Source from the content-addressed store, hash-verified

7578 any integer parts of the fraction (e.g. "10/4" becomes "2 1/2").
7579*/
7580QString QCPAxisTickerPi::fractionToString(int numerator, int denominator) const
7581{
7582 if (denominator == 0)
7583 {
7584 qDebug() << Q_FUNC_INFO << "called with zero denominator";
7585 return QString();
7586 }
7587 if (mFractionStyle == fsFloatingPoint) // should never be the case when calling this function
7588 {
7589 qDebug() << Q_FUNC_INFO << "shouldn't be called with fraction style fsDecimal";
7590 return QString::number(numerator/double(denominator)); // failsafe
7591 }
7592 int sign = numerator*denominator < 0 ? -1 : 1;
7593 numerator = qAbs(numerator);
7594 denominator = qAbs(denominator);
7595
7596 if (denominator == 1)
7597 {
7598 return QString::number(sign*numerator);
7599 } else
7600 {
7601 int integerPart = numerator/denominator;
7602 int remainder = numerator%denominator;
7603 if (remainder == 0)
7604 {
7605 return QString::number(sign*integerPart);
7606 } else
7607 {
7608 if (mFractionStyle == fsAsciiFractions)
7609 {
7610 return QString(QLatin1String("%1%2%3/%4"))
7611 .arg(sign == -1 ? QLatin1String("-") : QLatin1String(""))
7612 .arg(integerPart > 0 ? QString::number(integerPart)+QLatin1String(" ") : QString(QLatin1String("")))
7613 .arg(remainder)
7614 .arg(denominator);
7615 } else if (mFractionStyle == fsUnicodeFractions)
7616 {
7617 return QString(QLatin1String("%1%2%3"))
7618 .arg(sign == -1 ? QLatin1String("-") : QLatin1String(""))
7619 .arg(integerPart > 0 ? QString::number(integerPart) : QLatin1String(""))
7620 .arg(unicodeFraction(remainder, denominator));
7621 }
7622 }
7623 }
7624 return QString();
7625}
7626
7627/*! \internal
7628

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected