X-Git-Url: http://sigrok.org/gitweb/?p=pulseview.git;a=blobdiff_plain;f=pv%2Futil.cpp;fp=pv%2Futil.cpp;h=49b9467c1642737a1a995df5adcb622e2bbddfa1;hp=6424a9b1b5893b13d212ad1deef61221bea30aef;hb=0cbadf1c6a80ec37481ae11ee3cb709eeffda3e7;hpb=2795de2e7fdaeb6e9f672e0dc73dea328f35e585 diff --git a/pv/util.cpp b/pv/util.cpp index 6424a9b1..49b9467c 100644 --- a/pv/util.cpp +++ b/pv/util.cpp @@ -144,6 +144,41 @@ QString format_time_si(const Timestamp& v, SIPrefix prefix, return s; } +QString format_value_si(double v, SIPrefix prefix, unsigned precision, + QString unit, bool sign) +{ + if (prefix == SIPrefix::unspecified) { + // No prefix given, calculate it + + if (v == 0) { + prefix = SIPrefix::none; + } else { + int exp = exponent(SIPrefix::yotta); + prefix = SIPrefix::yocto; + while ((fabs(v) * pow(Timestamp(10), exp)) > 999 && + prefix < SIPrefix::yotta) { + prefix = successor(prefix); + exp -= 3; + } + } + } + + assert(prefix >= SIPrefix::yocto); + assert(prefix <= SIPrefix::yotta); + + const double multiplier = pow(10.0, -exponent(prefix)); + + QString s; + QTextStream ts(&s); + if (sign && (v != 0)) + ts << forcesign; + ts.setRealNumberNotation(QTextStream::FixedNotation); + ts.setRealNumberPrecision(precision); + ts << (v * multiplier) << ' ' << prefix << unit; + + return s; +} + QString format_time_si_adjusted(const Timestamp& t, SIPrefix prefix, unsigned precision, QString unit, bool sign) {