From: Joel Holdsworth Date: Tue, 30 Dec 2014 09:56:49 +0000 (+0000) Subject: util: Added format_si_value X-Git-Tag: pulseview-0.3.0~290 X-Git-Url: https://sigrok.org/gitweb/?p=pulseview.git;a=commitdiff_plain;h=4bc10a910593fda2d378d62a57448cec4b2166bf util: Added format_si_value --- diff --git a/pv/util.cpp b/pv/util.cpp index 62f0ee1f..2a04aaec 100644 --- a/pv/util.cpp +++ b/pv/util.cpp @@ -36,9 +36,21 @@ static const QString SIPrefixes[9] = {"f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G"}; const int FirstSIPrefixPower = -15; -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,26 +58,23 @@ 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"; + << (v * multiplier) << SIPrefixes[prefix] << unit; return s; } -QString format_second(double second) +QString format_time(double t, int prefix, + unsigned int precision, bool sign) { - unsigned int i = 0; - int exp = - FirstSIPrefixPower; - - while ((second * pow(10.0, exp)) > 999.0 && i < countof(SIPrefixes) - 1) { - i++; - exp -= 3; - } + return format_si_value(t, "s", prefix, precision, sign); +} - return format_time(second, i, 0, false); +QString format_second(double second) +{ + return format_si_value(second, "s", -1, 0, false); } } // namespace util diff --git a/pv/util.hpp b/pv/util.hpp index 572bed31..5d656940 100644 --- a/pv/util.hpp +++ b/pv/util.hpp @@ -31,18 +31,33 @@ namespace util { extern const int FirstSIPrefixPower; /** - * Formats a given time value with the specified SI prefix. + * Formats a given value with the specified SI prefix. + * @param v The value to format. + * @param unit The unit of quantity. + * @param prefix The number of the prefix, from 0 for 'femto' up to + * 8 for 'giga'. If prefix is set to -1, the prefix will be calculated. + * @param precision The number of digits after the decimal separator. + * @param sign Whether or not to add a sign also for positive numbers. + * + * @return The formated value. + */ +QString format_si_value( + double v, QString unit, int prefix = -1, + unsigned precision = 0, bool sign = true); + +/** + * Formats a given time with the specified SI prefix. * @param t The time value in seconds to format. * @param prefix The number of the prefix, from 0 for 'femto' up to - * 8 for 'giga'. - * @parma precision The number of digits after the decimal separator. + * 8 for 'giga'. If prefix is set to -1, the prefix will be calculated. + * @param unit The unit of quantity. + * @param precision The number of digits after the decimal separator. * @param sign Whether or not to add a sign also for positive numbers. * * @return The formated value. */ QString format_time( - double t, unsigned int prefix, - unsigned precision = 0, bool sign = true); + double t, int prefix = -1, unsigned precision = 0, bool sign = true); /** * Formats a given time value with a SI prefix so that the