From 6552a48639c7583d31a107a529ef0c51acf83f91 Mon Sep 17 00:00:00 2001 From: Klaas Freitag Date: Wed, 24 Jul 2013 14:36:38 +0200 Subject: [PATCH] Make compactFormatDouble a bit smarter. --- src/mirall/utility.cpp | 13 ++++++++++++- src/mirall/utility.h | 11 ++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/mirall/utility.cpp b/src/mirall/utility.cpp index 862d54621..e88fd25be 100644 --- a/src/mirall/utility.cpp +++ b/src/mirall/utility.cpp @@ -108,10 +108,19 @@ QString Utility::octetsToString( qint64 octets ) static const qint64 tb = 1024 * gb; if (octets >= tb) { + if (octets < 10*tb) { + return compactFormatDouble(double(octets)/double(tb), 1, QLatin1String("TB")); + } return QString::number(octets/tb) + QLatin1String(" TB"); } else if (octets >= gb) { + if (octets < 10*gb) { + return compactFormatDouble(double(octets)/double(gb), 1, QLatin1String("GB")); + } return QString::number(octets/gb) + QLatin1String(" GB"); } else if (octets >= mb) { + if (octets < 10*mb) { + return compactFormatDouble(double(octets)/double(mb), 1, QLatin1String("MB")); + } return QString::number(octets/mb) + QLatin1String(" MB"); } else if (octets >= kb) { return QString::number(octets/kb) + QLatin1String(" KB"); @@ -319,7 +328,7 @@ qint64 Utility::freeDiskSpace(const QString &path, bool *ok) #endif } -QString Utility::compactFormatDouble(double value, int prec) +QString Utility::compactFormatDouble(double value, int prec, const QString& unit) { QLocale locale = QLocale::system(); QChar decPoint = locale.decimalPoint(); @@ -331,6 +340,8 @@ QString Utility::compactFormatDouble(double value, int prec) } str.chop(1); } + if( !unit.isEmpty() ) + str += (QLatin1Char(' ')+unit); return str; } diff --git a/src/mirall/utility.h b/src/mirall/utility.h index 0054cfb9b..4bed2f15c 100644 --- a/src/mirall/utility.h +++ b/src/mirall/utility.h @@ -33,7 +33,16 @@ namespace Utility void setLaunchOnStartup(const QString &appName, const QString& guiName, bool launch); qint64 freeDiskSpace(const QString &path, bool *ok = 0); /** Like QLocale::toString(double, 'f', prec), but drops trailing zeros after the decimal point */ - QString compactFormatDouble(double value, int prec); + + /** + * @brief compactFormatDouble - formats a double value human readable. + * + * @param value the value to format. + * @param prec the precision. + * @param unit an optional unit that is appended if present. + * @return the formatted string. + */ + QString compactFormatDouble(double value, int prec, const QString& unit = QString::null); } }