X-Git-Url: http://git.code-monkey.de/?p=pulseview.git;a=blobdiff_plain;f=pv%2Futil.cpp;h=49b9467c1642737a1a995df5adcb622e2bbddfa1;hp=a452aac4b6eaef7e51f85b9cd6d0b0f37f7b5e19;hb=f4ab4b5c657e5613caba82feaa81a8a400e4f331;hpb=34f4a40baa0b4485ebee71d51c0a3bf70a0bad8b diff --git a/pv/util.cpp b/pv/util.cpp index a452aac..49b9467 100644 --- a/pv/util.cpp +++ b/pv/util.cpp @@ -144,6 +144,41 @@ QString format_time_si(const Timestamp& v, SIPrefix prefix, 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; +} + QString format_time_si_adjusted(const Timestamp& t, SIPrefix prefix, unsigned precision, QString unit, bool sign) { @@ -225,12 +260,12 @@ QString format_time_minutes(const Timestamp& t, signed precision, bool sign) } /** - * Split a string into tokens at occurances of the separator. + * Split a string into tokens at occurences of the separator. * - * @param[in] text the input string to split - * @param[in] separator the delimiter between tokens + * @param[in] text The input string to split. + * @param[in] separator The delimiter between tokens. * - * @return a vector of broken down tokens + * @return A vector of broken down tokens. */ vector split_string(string text, string separator) {