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.
This commit is contained in:
Alexander Tkachev 2019-07-24 00:45:20 +07:00 committed by Matan Bareket
parent 16d97b6948
commit 7fc6477ce2

View file

@ -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