projects
/
pulseview.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
util: Added format_si_value
[pulseview.git]
/
pv
/
util.cpp
diff --git
a/pv/util.cpp
b/pv/util.cpp
index 62f0ee1f67002b33ef2816bef15ccd3679dc4a00..2a04aaec7d99e68fb7ef9295c6ec4e40f5476425 100644
(file)
--- 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;
{"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)
{
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,
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);
QString s;
QTextStream ts(&s);
- if (sign)
{
+ if (sign)
ts << forcesign;
ts << forcesign;
- }
ts << fixed << qSetRealNumberPrecision(precision)
ts << fixed << qSetRealNumberPrecision(precision)
- << (
t * multiplier) << SIPrefixes[prefix] << "s"
;
+ << (
v * multiplier) << SIPrefixes[prefix] << unit
;
return s;
}
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
}
} // namespace util