From 7fc6477ce2d1001ab5111a16d2a6b408951a0b59 Mon Sep 17 00:00:00 2001 From: Alexander Tkachev Date: Wed, 24 Jul 2019 00:45:20 +0700 Subject: COMMON: Update getHumanReadableBytes() in util.h Function now casts bytes (as <1024) to unsigned long int to correspond "%lu" format string. For consistency, KB are now printed as floating number. Finally, it looks like double is pretty precise to be used in comparisons, so I made the function a little bit shorter. --- common/util.cpp | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) (limited to 'common') diff --git a/common/util.cpp b/common/util.cpp index 7c309ce364..4b7537f8ec 100644 --- a/common/util.cpp +++ b/common/util.cpp @@ -168,38 +168,31 @@ bool isGraph(int c) { Common::String getHumanReadableBytes(uint64 bytes, Common::String &unitsOut) { - Common::String result = Common::String::format("%lu", bytes); - unitsOut = "B"; - - if (bytes >= 1024) { - bytes /= 1024; - result = Common::String::format("%lu", bytes); - unitsOut = "KB"; + if (bytes < 1024) { + unitsOut = "B"; + return Common::String::format("%lu", (unsigned long int)bytes); } - double floating = bytes; + double floating = bytes / 1024.0; + unitsOut = "KB"; - if (bytes >= 1024) { - bytes /= 1024; + if (floating >= 1024) { floating /= 1024.0; unitsOut = "MB"; } - if (bytes >= 1024) { - bytes /= 1024; + if (floating >= 1024) { floating /= 1024.0; unitsOut = "GB"; } - if (bytes >= 1024) { // woah - bytes /= 1024; + if (floating >= 1024) { // woah floating /= 1024.0; unitsOut = "TB"; } // print one digit after floating point - result = Common::String::format("%.1f", floating); - return result; + return Common::String::format("%.1f", floating); } } // End of namespace Common -- cgit v1.2.3