src/miscallenous.cpp

changeset 1066
d7ec12688716
parent 1065
c8ecddbd99e9
child 1067
819973864620
--- a/src/miscallenous.cpp	Sat Jan 28 14:14:28 2017 +0200
+++ b/src/miscallenous.cpp	Sat Jan 28 14:33:09 2017 +0200
@@ -58,11 +58,17 @@
 }
 
 
-void roundToDecimals (double& a, int decimals)
+void roundToDecimals(double& value, int decimals)
 {
-	static const double factors[] = { 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9 };
-	if (decimals >= 0 and decimals < countof(factors))
-		a = round (a * factors[decimals]) / factors[decimals];
+	if (decimals == 0)
+	{
+		value = round(value);
+	}
+	else if (decimals > 0)
+	{
+		qreal coefficient = pow(10, decimals);
+		value = round(value * coefficient) / coefficient;
+	}
 }
 
 
@@ -81,12 +87,8 @@
 
 QString formatFileSize (qint64 size)
 {
-	if (size < 1024LL)
-		return QString::number (size) + " bytes";
-	else if (size < (1024LL * 1024LL))
-		return QString::number (double (size) / 1024LL, 'f', 1) + " Kb";
-	else if (size < (1024LL * 1024LL * 1024LL))
-		return QString::number (double (size) / (1024LL * 1024LL), 'f', 1) + " Mb";
-	else
-		return QString::number (double (size) / (1024LL * 1024LL * 1024LL), 'f', 1) + " Gb";
+	static const QString suffixes[] = {" bytes", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
+	int magnitude = floor(log10(size) + 1e-10);
+	magnitude = qMin(magnitude, countof(suffixes));
+	return QString::number(size) + suffixes[magnitude];
 }

mercurial