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