1 /////////////////////////////////////////////////////////////////////////////
2 // Name: numformatter.cpp
3 // Purpose: wxNumberFormatter
4 // Author: Fulvio Senore, Vadim Zeitlin
6 // Copyright: (c) 2010 wxWidgets team
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
21 #include "wx/numformatter.h"
24 // ============================================================================
25 // wxNumberFormatter implementation
26 // ============================================================================
28 // ----------------------------------------------------------------------------
29 // Locale information accessors
30 // ----------------------------------------------------------------------------
32 wxChar
wxNumberFormatter::GetDecimalSeparator()
34 // Notice that while using static variable here is not MT-safe, the worst
35 // that can happen is that we redo the initialization if we're called
36 // concurrently from more than one thread so it's not a real problem.
37 static wxChar s_decimalSeparator
= 0;
39 if ( !s_decimalSeparator
)
42 s
= wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
);
45 // We really must have something for decimal separator, so fall
46 // back to the C locale default.
47 s_decimalSeparator
= '.';
51 // To the best of my knowledge there are no locales like this.
52 wxASSERT_MSG( s
.length() == 1,
53 "Multi-character decimal separator?" );
55 s_decimalSeparator
= s
[0];
59 return s_decimalSeparator
;
62 bool wxNumberFormatter::GetThousandsSeparatorIfUsed(wxChar
*sep
)
64 static wxChar s_thousandsSeparator
= 0;
65 static bool s_initialized
= false;
70 s
= wxLocale::GetInfo(wxLOCALE_THOUSANDS_SEP
, wxLOCALE_CAT_NUMBER
);
73 wxASSERT_MSG( s
.length() == 1,
74 "Multi-character thousands separator?" );
76 s_thousandsSeparator
= s
[0];
78 //else: Unlike above it's perfectly fine for the thousands separator to
79 // be empty if grouping is not used, so just leave it as 0.
84 if ( !s_thousandsSeparator
)
88 *sep
= s_thousandsSeparator
;
93 // ----------------------------------------------------------------------------
94 // Conversion to string and helpers
95 // ----------------------------------------------------------------------------
97 wxString
wxNumberFormatter::ToString(long val
, int style
)
99 wxString s
= wxString::Format("%ld", val
);
101 if ( style
& Style_WithThousandsSep
)
102 AddThousandsSeparators(s
);
104 wxASSERT_MSG( !(style
& Style_NoTrailingZeroes
),
105 "Style_NoTrailingZeroes can't be used with integer values" );
110 wxString
wxNumberFormatter::ToString(double val
, int precision
, int style
)
112 const wxString fmt
= wxString::Format("%%.%df", precision
);
113 wxString s
= wxString::Format(fmt
, val
);
115 if ( style
& Style_WithThousandsSep
)
116 AddThousandsSeparators(s
);
118 if ( style
& Style_NoTrailingZeroes
)
119 RemoveTrailingZeroes(s
);
124 void wxNumberFormatter::AddThousandsSeparators(wxString
& s
)
127 if ( !GetThousandsSeparatorIfUsed(&thousandsSep
) )
130 size_t pos
= s
.find(GetDecimalSeparator());
131 if ( pos
== wxString::npos
)
133 // Start grouping at the end of an integer number.
137 // We currently group digits by 3 independently of the locale. This is not
138 // the right thing to do and we should use lconv::grouping (under POSIX)
139 // and GetLocaleInfo(LOCALE_SGROUPING) (under MSW) to get information about
140 // the correct grouping to use. This is something that needs to be done at
141 // wxLocale level first and then used here in the future (TODO).
142 const size_t GROUP_LEN
= 3;
144 while ( pos
> GROUP_LEN
)
147 s
.insert(pos
, thousandsSep
);
151 void wxNumberFormatter::RemoveTrailingZeroes(wxString
& s
)
153 const size_t posDecSep
= s
.find(GetDecimalSeparator());
154 wxCHECK_RET( posDecSep
!= wxString::npos
, "No decimal separator" );
155 wxCHECK_RET( posDecSep
, "Can't start with decimal separator" );
157 // Find the last character to keep.
158 size_t posLastNonZero
= s
.find_last_not_of("0");
160 // If it's the decimal separator itself, don't keep it neither.
161 if ( posLastNonZero
== posDecSep
)
164 s
.erase(posLastNonZero
+ 1);
167 // ----------------------------------------------------------------------------
168 // Conversion from strings
169 // ----------------------------------------------------------------------------
171 void wxNumberFormatter::RemoveThousandsSeparators(wxString
& s
)
174 if ( !GetThousandsSeparatorIfUsed(&thousandsSep
) )
177 s
.Replace(wxString(thousandsSep
), wxString());
180 bool wxNumberFormatter::FromString(wxString s
, long *val
)
182 RemoveThousandsSeparators(s
);
183 return s
.ToLong(val
);
186 bool wxNumberFormatter::FromString(wxString s
, double *val
)
188 RemoveThousandsSeparators(s
);
189 return s
.ToDouble(val
);