X-Git-Url: https://sigrok.org/gitweb/?p=pulseview.git;a=blobdiff_plain;f=pv%2Futil.cpp;h=2e54adeaed3dffae88cb2b7354ce9eb57395302b;hp=28e0dfde50d2fb9230219c5bce3b18e58a70b2bc;hb=208c6fc371951fe74ba5a08a0950f2837eef8b7b;hpb=f0c9f81c03d2c1d1fa59c357a88d25f6a3128651 diff --git a/pv/util.cpp b/pv/util.cpp index 28e0dfde..2e54adea 100644 --- a/pv/util.cpp +++ b/pv/util.cpp @@ -18,7 +18,7 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "util.h" +#include "util.hpp" #include @@ -32,13 +32,26 @@ using namespace Qt; namespace pv { namespace util { -static const QString SIPrefixes[9] = - {"f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G"}; -const int FirstSIPrefixPower = -15; +static const QString SIPrefixes[17] = + {"y", "z", "a", "f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G", + "T", "P", "E", "Z", "Y"}; +const int FirstSIPrefixPower = -24; -QString format_time(double t, unsigned int prefix, +QString format_si_value(double v, QString unit, int prefix, unsigned int precision, bool sign) { + if (prefix < 0) { + int exp = -FirstSIPrefixPower; + + prefix = 0; + while ((fabs(v) * pow(10.0, exp)) > 999.0 && + prefix < (int)(countof(SIPrefixes) - 1)) { + prefix++; + exp -= 3; + } + } + + assert(prefix >= 0); assert(prefix < countof(SIPrefixes)); const double multiplier = pow(10.0, @@ -46,14 +59,24 @@ QString format_time(double t, unsigned int prefix, QString s; QTextStream ts(&s); - if (sign) { + if (sign) ts << forcesign; - } - ts << fixed << qSetRealNumberPrecision(precision) - << (t * multiplier) << SIPrefixes[prefix] << "s"; + ts << fixed << qSetRealNumberPrecision(precision) << + (v * multiplier) << " " << SIPrefixes[prefix] << unit; return s; } +QString format_time(double t, int prefix, + unsigned int precision, bool sign) +{ + return format_si_value(t, "s", prefix, precision, sign); +} + +QString format_second(double second) +{ + return format_si_value(second, "s", -1, 0, false); +} + } // namespace util } // namespace pv