diff options
-rw-r--r-- | examples/downloadspeed/main.cpp | 29 |
1 files changed, 17 insertions, 12 deletions
diff --git a/examples/downloadspeed/main.cpp b/examples/downloadspeed/main.cpp index 74686b510..2ed6651bf 100644 --- a/examples/downloadspeed/main.cpp +++ b/examples/downloadspeed/main.cpp @@ -40,20 +40,25 @@ #include <QtNetwork/QNetworkProxy> -static QString format(double data) +static QString humanReadableSize(quint64 intSize) { - if (data < 1024.0) - return QString::fromLatin1("%L1 B").arg(data); - data /= 1024.0; - if (data < 1024.0) - return QString::fromLatin1("%L1 KB").arg(data, 0, 'f', 2); - data /= 1024.0; - if (data < 1024.0) - return QString::fromLatin1("%L1 MB").arg(data, 0, 'f', 2); - data /= 1024.0; - return QString::fromLatin1("%L1 GB").arg(data, 0, 'f', 2); -} + QString unit; + double size; + + if (intSize < 1024 * 1024) { + size = 1. + intSize / 1024.; + unit = QObject::tr("kB"); + } else if (intSize < 1024 * 1024 * 1024) { + size = 1. + intSize / 1024. / 1024.; + unit = QObject::tr("MB"); + } else { + size = 1. + intSize / 1024. / 1024. / 1024.; + unit = QObject::tr("GB"); + } + size = qRound(size * 10) / 10.0; + return QString::fromLatin1("%L1 %2").arg(size, 0, 'g', 4).arg(unit); +} // -- Receiver |