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
This commit is contained in:
Vadim Zeitlin 2014-01-05 21:10:52 +00:00
parent d49096e10f
commit 419480cf22
2 changed files with 52 additions and 2 deletions

View File

@ -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";
}
// ----------------------------------------------------------------------------

View File

@ -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()