From 419480cf22a65f7c4d1df2d73f6dfb6e6c4bc6ae Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 5 Jan 2014 21:10:52 +0000 Subject: [PATCH] Fix several problems with number formatting in wxNumberFormatter. We shouldn't add thousands separators nor remove trailing zeros for the numbers in scientific format. Also avoid "-0" as output. See #15625. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@75560 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/common/numformatter.cpp | 16 ++++++++++++-- tests/strings/numformatter.cpp | 38 ++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/common/numformatter.cpp b/src/common/numformatter.cpp index 12de60e842..2428ef34b5 100644 --- a/src/common/numformatter.cpp +++ b/src/common/numformatter.cpp @@ -223,6 +223,10 @@ wxString wxNumberFormatter::ToString(double val, int precision, int style) void wxNumberFormatter::AddThousandsSeparators(wxString& s) { + // Thousands separators for numbers in scientific format are not relevant. + if ( s.find_first_of("eE") != wxString::npos ) + return; + wxChar thousandsSep; if ( !GetThousandsSeparatorIfUsed(&thousandsSep) ) return; @@ -254,9 +258,14 @@ void wxNumberFormatter::AddThousandsSeparators(wxString& s) void wxNumberFormatter::RemoveTrailingZeroes(wxString& s) { + // If number is in scientific format, trailing zeroes belong to the exponent and cannot be removed. + if ( s.find_first_of("eE") != wxString::npos ) + return; + const size_t posDecSep = s.find(GetDecimalSeparator()); - wxCHECK_RET( posDecSep != wxString::npos, - wxString::Format("No decimal separator in \"%s\"", s) ); + // No decimal point => removing trailing zeroes irrelevant for integer number. + if ( posDecSep == wxString::npos ) + return; wxCHECK_RET( posDecSep, "Can't start with decimal separator" ); // Find the last character to keep. @@ -267,6 +276,9 @@ void wxNumberFormatter::RemoveTrailingZeroes(wxString& s) posLastNonZero--; s.erase(posLastNonZero + 1); + // Remove sign from orphaned zero. + if ( s.compare("-0") == 0 ) + s = "0"; } // ---------------------------------------------------------------------------- diff --git a/tests/strings/numformatter.cpp b/tests/strings/numformatter.cpp index 9a3b2c5e7b..e88d0f38d0 100644 --- a/tests/strings/numformatter.cpp +++ b/tests/strings/numformatter.cpp @@ -147,6 +147,14 @@ void NumFormatterTestCase::DoubleToString() wxNumberFormatter::ToString(123456789., 1)); CPPUNIT_ASSERT_EQUAL("123,456,789.012", wxNumberFormatter::ToString(123456789.012, 3)); + CPPUNIT_ASSERT_EQUAL("12,345", + wxNumberFormatter::ToString(12345.012, -1)); + CPPUNIT_ASSERT_EQUAL("-123.1230", + wxNumberFormatter::ToString(-123.123, 4, wxNumberFormatter::Style_None)); + CPPUNIT_ASSERT_EQUAL("0.0", + wxNumberFormatter::ToString(0.02, 1, wxNumberFormatter::Style_None)); + CPPUNIT_ASSERT_EQUAL("-0.0", + wxNumberFormatter::ToString(-0.02, 1, wxNumberFormatter::Style_None)); } void NumFormatterTestCase::NoTrailingZeroes() @@ -194,6 +202,36 @@ void NumFormatterTestCase::NoTrailingZeroes() "123.456", wxNumberFormatter::ToString(123.456, 9, wxNumberFormatter::Style_NoTrailingZeroes) ); + + CPPUNIT_ASSERT_EQUAL + ( + "123.12", + wxNumberFormatter::ToString(123.123, 2, wxNumberFormatter::Style_NoTrailingZeroes) + ); + + CPPUNIT_ASSERT_EQUAL + ( + "123", + wxNumberFormatter::ToString(123.123, 0, wxNumberFormatter::Style_NoTrailingZeroes) + ); + + CPPUNIT_ASSERT_EQUAL + ( + "0", + wxNumberFormatter::ToString(-0.000123, 3, wxNumberFormatter::Style_NoTrailingZeroes) + ); + + CPPUNIT_ASSERT_EQUAL + ( + "123", + wxNumberFormatter::ToString(123., -1, wxNumberFormatter::Style_NoTrailingZeroes) + ); + + CPPUNIT_ASSERT_EQUAL + ( + "1e-120", + wxNumberFormatter::ToString(1e-120, -1, wxNumberFormatter::Style_NoTrailingZeroes) + ); } void NumFormatterTestCase::LongFromString()