diff options
author | evan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-15 18:22:57 +0000 |
---|---|---|
committer | evan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-15 18:22:57 +0000 |
commit | 26cfaf5d657b92f5f94eccc0a0b51c28caccbc5a (patch) | |
tree | 3f1c70fa80bce061afa2e655e32e7995f6a8517d /base/string_util.cc | |
parent | 5a6a7718ca4abee6709bf91cfbca76dc26eaecc3 (diff) | |
download | chromium_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/string_util.cc')
-rw-r--r-- | base/string_util.cc | 18 |
1 files changed, 6 insertions, 12 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]; |