X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=pv%2Futil.cpp;h=237ca7f059f1e589dac4dd9e6f6bc29120a6e3c5;hb=ef85cfa4599d3741e7007921e39a44490e97cfaf;hp=6424a9b1b5893b13d212ad1deef61221bea30aef;hpb=3083cda853bd12d73b741c3bc97b70f5d395a15a;p=pulseview.git diff --git a/pv/util.cpp b/pv/util.cpp index 6424a9b1..237ca7f0 100644 --- a/pv/util.cpp +++ b/pv/util.cpp @@ -138,8 +138,44 @@ QString format_time_si(const Timestamp& v, SIPrefix prefix, QTextStream ts(&s); if (sign && !v.is_zero()) ts << forcesign; - ts << qSetRealNumberPrecision(precision) << (v * multiplier) << ' ' - << prefix << unit; + ts << qSetRealNumberPrecision(precision) << (v * multiplier); + if (!unit.isNull()) + ts << ' ' << prefix << unit; + + 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; }