From 26cfaf5d657b92f5f94eccc0a0b51c28caccbc5a Mon Sep 17 00:00:00 2001 From: "evan@chromium.org" Date: Thu, 15 Apr 2010 18:22:57 +0000 Subject: 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 --- base/string_util.cc | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) (limited to 'base/string_util.cc') 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(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]; -- cgit v1.1