Make PrettySize less dumb

Originally committed to SVN as r6615.
This commit is contained in:
Thomas Goyne 2012-03-27 00:49:43 +00:00
parent 30f0a56832
commit fb7638312a
1 changed files with 9 additions and 19 deletions

View File

@ -86,33 +86,23 @@ wxString AegiIntegerToString(int value) {
/// @brief There shall be no kiB, MiB stuff here Pretty reading of size /// @brief There shall be no kiB, MiB stuff here Pretty reading of size
wxString PrettySize(int bytes) { wxString PrettySize(int bytes) {
// Suffixes const char *suffix[] = { "", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
wxArrayString suffix;
suffix.Add("");
suffix.Add(" kB");
suffix.Add(" MB");
suffix.Add(" GB");
suffix.Add(" TB");
suffix.Add(" PB");
// Set size // Set size
int i = 0; int i = 0;
double size = bytes; double size = bytes;
while (size > 1024) { while (size > 1024 && i < 9) {
size = size / 1024.0; size /= 1024.0;
i++; i++;
if (i == 6) {
i--;
break;
}
} }
// Set number of decimal places // Set number of decimal places
wxString final; const char *fmt = "%.0f";
if (size < 10) final = wxString::Format("%.2f",size); if (size < 10)
else if (size < 100) final = wxString::Format("%.1f",size); fmt = "%.2f";
else final = wxString::Format("%.0f",size); else if (size < 100)
return final + suffix[i]; fmt = "%1.f";
return wxString::Format(fmt, size) + " " + suffix[i];
} }
int SmallestPowerOf2(int x) { int SmallestPowerOf2(int x) {