Bug Fix - formatBytes

- Corrected code and tests regarding floating point rounding.
This commit is contained in:
Paul Beckingham 2009-06-27 20:39:33 -04:00
parent 029b2d1182
commit 52052f91f9
2 changed files with 7 additions and 5 deletions

View file

@ -34,7 +34,7 @@ Context context;
////////////////////////////////////////////////////////////////////////////////
int main (int argc, char** argv)
{
UnitTest t (19);
UnitTest t (21);
// TODO bool confirm (const std::string&);
// TODO int confirm3 (const std::string&);
@ -45,7 +45,9 @@ int main (int argc, char** argv)
// std::string formatBytes (size_t);
t.is (formatBytes (0), "0 B", "0 -> 0 B");
t.is (formatBytes (999), "999 B", "999 -> 999 B");
t.is (formatBytes (994), "994 B", "994 -> 994 B");
t.is (formatBytes (995), "1.0 KiB", "995 -> 1.0 KiB");
t.is (formatBytes (999), "1.0 KiB", "999 -> 1.0 KiB");
t.is (formatBytes (1000), "1.0 KiB", "1000 -> 1.0 KiB");
t.is (formatBytes (1001), "1.0 KiB", "1001 -> 1.0 KiB");

View file

@ -184,9 +184,9 @@ std::string formatBytes (size_t bytes)
{
char formatted[24];
if (bytes >= 1000000000) sprintf (formatted, "%.1f GiB", (bytes / 1000000000.0));
else if (bytes >= 1000000) sprintf (formatted, "%.1f MiB", (bytes / 1000000.0));
else if (bytes >= 1000) sprintf (formatted, "%.1f KiB", (bytes / 1000.0));
if (bytes >= 995000000) sprintf (formatted, "%.1f GiB", (bytes / 1000000000.0));
else if (bytes >= 995000) sprintf (formatted, "%.1f MiB", (bytes / 1000000.0));
else if (bytes >= 995) sprintf (formatted, "%.1f KiB", (bytes / 1000.0));
else sprintf (formatted, "%d B", (int)bytes );
return commify (formatted);