* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
-#include "util.h"
+#include "util.hpp"
#include <extdef.h>
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)
{
- assert(prefix < countof(SIPrefixes));
+ 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 < (int)countof(SIPrefixes));
const double multiplier = pow(10.0,
(int)- prefix * 3 - FirstSIPrefixPower);
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_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