summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorevan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-04-15 18:22:57 +0000
committerevan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-04-15 18:22:57 +0000
commit26cfaf5d657b92f5f94eccc0a0b51c28caccbc5a (patch)
tree3f1c70fa80bce061afa2e655e32e7995f6a8517d /base
parent5a6a7718ca4abee6709bf91cfbca76dc26eaecc3 (diff)
downloadchromium_src-26cfaf5d657b92f5f94eccc0a0b51c28caccbc5a.zip
chromium_src-26cfaf5d657b92f5f94eccc0a0b51c28caccbc5a.tar.gz
chromium_src-26cfaf5d657b92f5f94eccc0a0b51c28caccbc5a.tar.bz2
Rejigger how we format the display of bytes.
(Note that this does *not* touch the mega/mebi issue, despite my recent discussion of it.) What had been bugging me was that when downloading a file, you'd see it count up 10.7, 10.8, 10.9, *11*, 11.1. That is, previously when we rounded to displaying a trailing zero, we'd clip it off, which would make a download display like "11.1 / 50.2MB" jiggle to the left every time the trailing digit lined up. Now we try to always display three digits, which means we count up like 98.8, 98.9, *99.0*, 99.1, ... 99.9, 100, ... 101. This code is after fiddling the test into the shape I wanted it to be, then making the code produce that behavior. TEST=covered by unit test, updated with new expected behavior Review URL: http://codereview.chromium.org/1558025 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@44671 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/string_util.cc18
-rw-r--r--base/string_util_unittest.cc24
2 files changed, 24 insertions, 18 deletions
diff --git a/base/string_util.cc b/base/string_util.cc
index 494d09d..fafc106 100644
--- a/base/string_util.cc
+++ b/base/string_util.cc
@@ -887,19 +887,13 @@ std::wstring FormatBytesInternal(int64 bytes,
for (int i = 0; i < units; ++i)
unit_amount /= 1024.0;
- wchar_t tmp[64];
- // If the first decimal digit is 0, don't show it.
- double int_part;
- double fractional_part = modf(unit_amount, &int_part);
- modf(fractional_part * 10, &int_part);
- if (int_part == 0) {
- base::swprintf(tmp, arraysize(tmp),
- L"%lld", static_cast<int64>(unit_amount));
- } else {
- base::swprintf(tmp, arraysize(tmp), L"%.1lf", unit_amount);
- }
+ wchar_t buf[64];
+ if (bytes != 0 && units != DATA_UNITS_BYTE && unit_amount < 100)
+ base::swprintf(buf, arraysize(buf), L"%.1lf", unit_amount);
+ else
+ base::swprintf(buf, arraysize(buf), L"%.0lf", unit_amount);
- std::wstring ret(tmp);
+ std::wstring ret(buf);
if (show_units) {
ret += L" ";
ret += suffix[units];
diff --git a/base/string_util_unittest.cc b/base/string_util_unittest.cc
index 8fc8f15..4602344 100644
--- a/base/string_util_unittest.cc
+++ b/base/string_util_unittest.cc
@@ -362,20 +362,32 @@ TEST(StringUtilTest, FormatBytes) {
const wchar_t* expected;
const wchar_t* expected_with_units;
} cases[] = {
+ // Expected behavior: we show one post-decimal digit when we have
+ // under two pre-decimal digits, except in cases where it makes no
+ // sense (zero or bytes).
+ // Since we switch units once we cross the 1000 mark, this keeps
+ // the display of file sizes or bytes consistently around three
+ // digits.
{0, DATA_UNITS_BYTE, L"0", L"0 B"},
{512, DATA_UNITS_BYTE, L"512", L"512 B"},
{512, DATA_UNITS_KIBIBYTE, L"0.5", L"0.5 kB"},
{1024*1024, DATA_UNITS_KIBIBYTE, L"1024", L"1024 kB"},
- {1024*1024, DATA_UNITS_MEBIBYTE, L"1", L"1 MB"},
- {1024*1024*1024, DATA_UNITS_GIBIBYTE, L"1", L"1 GB"},
- {10LL*1024*1024*1024, DATA_UNITS_GIBIBYTE, L"10", L"10 GB"},
+ {1024*1024, DATA_UNITS_MEBIBYTE, L"1.0", L"1.0 MB"},
+ {1024*1024*1024, DATA_UNITS_GIBIBYTE, L"1.0", L"1.0 GB"},
+ {10LL*1024*1024*1024, DATA_UNITS_GIBIBYTE, L"10.0", L"10.0 GB"},
+ {99LL*1024*1024*1024, DATA_UNITS_GIBIBYTE, L"99.0", L"99.0 GB"},
+ {105LL*1024*1024*1024, DATA_UNITS_GIBIBYTE, L"105", L"105 GB"},
+ {105LL*1024*1024*1024 + 500LL*1024*1024, DATA_UNITS_GIBIBYTE,
+ L"105", L"105 GB"},
{~(1LL<<63), DATA_UNITS_GIBIBYTE, L"8589934592", L"8589934592 GB"},
- // Make sure the first digit of the fractional part works.
- {1024*1024 + 103, DATA_UNITS_KIBIBYTE, L"1024.1", L"1024.1 kB"},
+
+ {99*1024 + 103, DATA_UNITS_KIBIBYTE, L"99.1", L"99.1 kB"},
+ {1024*1024 + 103, DATA_UNITS_KIBIBYTE, L"1024", L"1024 kB"},
{1024*1024 + 205 * 1024, DATA_UNITS_MEBIBYTE, L"1.2", L"1.2 MB"},
{1024*1024*1024 + (927 * 1024*1024), DATA_UNITS_GIBIBYTE,
L"1.9", L"1.9 GB"},
- {10LL*1024*1024*1024, DATA_UNITS_GIBIBYTE, L"10", L"10 GB"},
+ {10LL*1024*1024*1024, DATA_UNITS_GIBIBYTE, L"10.0", L"10.0 GB"},
+ {100LL*1024*1024*1024, DATA_UNITS_GIBIBYTE, L"100", L"100 GB"},
#ifdef NDEBUG
{-1, DATA_UNITS_BYTE, L"", L""},
#endif