From 669df123648aaa6aeccc70206b5417bc48b4e9ae Mon Sep 17 00:00:00 2001 From: Daniel Wilhelm Date: Fri, 18 Apr 2014 17:26:50 +0200 Subject: 5.19 --- zen/format_unit.cpp | 50 ++++++++++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 26 deletions(-) (limited to 'zen/format_unit.cpp') diff --git a/zen/format_unit.cpp b/zen/format_unit.cpp index 9e5975d9..4f16e3e1 100644 --- a/zen/format_unit.cpp +++ b/zen/format_unit.cpp @@ -24,47 +24,45 @@ using namespace zen; +std::wstring zen::formatThreeDigitPrecision(double value) +{ + //print at least three digits: 0,01 | 0,11 | 1,11 | 11,1 | 111 + if (numeric::abs(value) < 9.995) //9.999 must not be formatted as "10.00" + return printNumber(L"%.2f", value); + if (numeric::abs(value) < 99.95) //99.99 must not be formatted as "100.0" + return printNumber(L"%.1f", value); + return numberTo(numeric::round(value)); +} + + std::wstring zen::filesizeToShortString(Int64 size) { //if (size < 0) return _("Error"); -> really? if (numeric::abs(size) <= 999) - return replaceCpy(_P("1 Byte", "%x Bytes", to(size)), L"%x", numberTo(size)); - - auto formatUnitSize = [](double sizeInUnit, const std::wstring& unitTxt) -> std::wstring - { - //print just three significant digits: 0,01 | 0,11 | 1,11 | 11,1 | 111 - const size_t fullunits = static_cast(numeric::abs(sizeInUnit)); - const int precisionDigits = fullunits < 10 ? 2 : fullunits < 100 ? 1 : 0; //sprintf requires "int" - - wchar_t buffer[50]; -#ifdef __MINGW32__ - int charsWritten = ::_snwprintf(buffer, 50, L"%.*f", precisionDigits, sizeInUnit); //MinGW does not comply to the C standard here -#else - int charsWritten = std::swprintf(buffer, 50, L"%.*f", precisionDigits, sizeInUnit); -#endif - return charsWritten > 0 ? replaceCpy(unitTxt, L"%x", std::wstring(buffer, charsWritten)) : _("Error"); - }; + return replaceCpy(_P("1 byte", "%x bytes", to(size)), L"%x", numberTo(size)); double sizeInUnit = to(size); + auto formatUnit = [&](const std::wstring& unitTxt) { return replaceCpy(unitTxt, L"%x", formatThreeDigitPrecision(sizeInUnit)); }; + sizeInUnit /= 1024; - if (numeric::abs(sizeInUnit) <= 999) - return formatUnitSize(sizeInUnit, _("%x KB")); + if (numeric::abs(sizeInUnit) < 999.5) + return formatUnit(_("%x KB")); sizeInUnit /= 1024; - if (numeric::abs(sizeInUnit) <= 999) - return formatUnitSize(sizeInUnit, _("%x MB")); + if (numeric::abs(sizeInUnit) < 999.5) + return formatUnit(_("%x MB")); sizeInUnit /= 1024; - if (numeric::abs(sizeInUnit) <= 999) - return formatUnitSize(sizeInUnit, _("%x GB")); + if (numeric::abs(sizeInUnit) < 999.5) + return formatUnit(_("%x GB")); sizeInUnit /= 1024; - if (numeric::abs(sizeInUnit) <= 999) - return formatUnitSize(sizeInUnit, _("%x TB")); + if (numeric::abs(sizeInUnit) < 999.5) + return formatUnit(_("%x TB")); sizeInUnit /= 1024; - return formatUnitSize(sizeInUnit, _("%x PB")); + return formatUnit(_("%x PB")); } @@ -148,7 +146,7 @@ std::wstring zen::remainingTimeToString(double timeInSec) std::wstring zen::fractionToString(double fraction) { - return printNumber(L"%3.2f", fraction * 100.0) + L'%'; //no need to internationalize fraction!? + return printNumber(L"%.2f", fraction * 100.0) + L'%'; //no need to internationalize fraction!? } -- cgit