From 4915bbf8f342b7225f215fd9b0d3fb252c843b9e Mon Sep 17 00:00:00 2001 From: Christian Kamm Date: Thu, 21 Jan 2016 09:50:32 +0100 Subject: [PATCH] Adjust size description strings #3403 @jancorchardt suggested to not have decimal places on file sizes, except for GB. --- src/libsync/utility.cpp | 13 ++++++++++++- test/testutility.h | 8 ++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/libsync/utility.cpp b/src/libsync/utility.cpp index 6fca88232..613c7affb 100644 --- a/src/libsync/utility.cpp +++ b/src/libsync/utility.cpp @@ -113,6 +113,10 @@ QString Utility::octetsToString( qint64 octets ) QString s; qreal value = octets; + // Whether we care about decimals: only for GB and only + // if it's less than 10 GB. + bool round = true; + // do not display terra byte with the current units, as when // the MB, GB and KB units were made, there was no TB, // see the JEDEC standard @@ -120,6 +124,7 @@ QString Utility::octetsToString( qint64 octets ) if (octets >= gb) { s = QCoreApplication::translate("Utility", "%L1 GB"); value /= gb; + round = false; } else if (octets >= mb) { s = QCoreApplication::translate("Utility", "%L1 MB"); value /= mb; @@ -130,7 +135,13 @@ QString Utility::octetsToString( qint64 octets ) s = QCoreApplication::translate("Utility", "%L1 B"); } - return (value > 9.95) ? s.arg(qRound(value)) : s.arg(value, 0, 'g', 2); + if (value > 9.95) + round = true; + + if (round) + return s.arg(qRound(value)); + + return s.arg(value, 0, 'g', 2); } // Qtified version of get_platforms() in csync_owncloud.c diff --git a/test/testutility.h b/test/testutility.h index e8ce07528..bc7cc4d6c 100644 --- a/test/testutility.h +++ b/test/testutility.h @@ -31,14 +31,14 @@ private slots: QLocale::setDefault(QLocale("en")); QCOMPARE(octetsToString(999) , QString("999 B")); QCOMPARE(octetsToString(1024) , QString("1 KB")); - QCOMPARE(octetsToString(1364) , QString("1.3 KB")); + QCOMPARE(octetsToString(1364) , QString("1 KB")); - QCOMPARE(octetsToString(9110) , QString("8.9 KB")); - QCOMPARE(octetsToString(9910) , QString("9.7 KB")); + QCOMPARE(octetsToString(9110) , QString("9 KB")); + QCOMPARE(octetsToString(9910) , QString("10 KB")); QCOMPARE(octetsToString(10240) , QString("10 KB")); QCOMPARE(octetsToString(123456) , QString("121 KB")); - QCOMPARE(octetsToString(1234567) , QString("1.2 MB")); + QCOMPARE(octetsToString(1234567) , QString("1 MB")); QCOMPARE(octetsToString(12345678) , QString("12 MB")); QCOMPARE(octetsToString(123456789) , QString("118 MB")); QCOMPARE(octetsToString(1000LL*1000*1000 * 5) , QString("4.7 GB"));