]> sigrok.org Git - pulseview.git/blobdiff - pv/util.cpp
Make the analog value voltage display more sane
[pulseview.git] / pv / util.cpp
index 9a9a5065a2e17180eb6cdebb71b4aefc07fb5833..897254e12160fa59e60c7766efbd5e84f1d5fff5 100644 (file)
@@ -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<double>());
+
        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) :