Refactor roundToDecimals and formatFileSize

Sat, 28 Jan 2017 14:33:09 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 28 Jan 2017 14:33:09 +0200
changeset 1066
d7ec12688716
parent 1065
c8ecddbd99e9
child 1067
819973864620

Refactor roundToDecimals and formatFileSize

src/miscallenous.cpp file | annotate | diff | comparison | revisions
src/miscallenous.h file | annotate | diff | comparison | revisions
--- 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];
 }
--- a/src/miscallenous.h	Sat Jan 28 14:14:28 2017 +0200
+++ b/src/miscallenous.h	Sat Jan 28 14:33:09 2017 +0200
@@ -34,5 +34,5 @@
 QString formatFileSize (qint64 size);
 int gcd (int a, int b);
 QString joinStrings (QList<StringFormatArg> vals, QString delim = " ");
-void roundToDecimals (double& a, int decimals);
+void roundToDecimals (double& value, int decimals);
 void simplify (int& numer, int& denom);

mercurial