X-Git-Url: https://sigrok.org/gitweb/?p=pulseview.git;a=blobdiff_plain;f=pv%2Futil.cpp;h=897254e12160fa59e60c7766efbd5e84f1d5fff5;hp=9a9a5065a2e17180eb6cdebb71b4aefc07fb5833;hb=0efa7f0cc59eab39dccce698d8fb6675feeb3e73;hpb=8845be3c9c7d5aca02fb2efc4038f4735a5242d6 diff --git a/pv/util.cpp b/pv/util.cpp index 9a9a5065..897254e1 100644 --- a/pv/util.cpp +++ b/pv/util.cpp @@ -110,25 +110,31 @@ static QTextStream& operator<<(QTextStream& stream, const Timestamp& t) return stream << QString::fromStdString(str); } -QString format_time_si(const Timestamp& v, SIPrefix prefix, - unsigned int precision, QString unit, bool sign) +SIPrefix determine_value_prefix(double v) { - if (prefix == SIPrefix::unspecified) { - // No prefix given, calculate it - - if (v.is_zero()) { - 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; - } + SIPrefix prefix; + + if (v == 0) { + prefix = SIPrefix::none; + } else { + int exp = exponent(SIPrefix::yotta); + prefix = SIPrefix::yocto; + while ((fabs(v) * pow(10, exp)) > 999 && + prefix < SIPrefix::yotta) { + prefix = successor(prefix); + exp -= 3; } } + return prefix; +} + +QString format_time_si(const Timestamp& v, SIPrefix prefix, + unsigned int precision, QString unit, bool sign) +{ + if (prefix == SIPrefix::unspecified) + prefix = determine_value_prefix(v.convert_to()); + assert(prefix >= SIPrefix::yocto); assert(prefix <= SIPrefix::yotta); @@ -148,19 +154,7 @@ 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; - } - } + prefix = determine_value_prefix(v); const int prefix_order = -exponent(prefix); precision = (prefix >= SIPrefix::none) ? max((int)(precision + prefix_order), 0) :