1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/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 #include <locale.h> // for setlocale and LC_ALL
26 // ----------------------------------------------------------------------------
28 // ----------------------------------------------------------------------------
33 // Contains information about the locale which was used to initialize our
34 // cached values of the decimal and thousands separators. Notice that it isn't
35 // enough to store just wxLocale because the user code may call setlocale()
36 // directly and storing just C locale string is not enough because we can use
37 // the OS API directly instead of the CRT ones on some platforms. So just store
56 // Return true if this is the first time this function is called for this
57 // object or if the program locale has changed since the last time it was
58 // called. Otherwise just return false indicating that updating locale-
59 // dependent information is not necessary.
60 bool NotInitializedOrHasChanged()
62 wxLocale
* const wxloc
= wxGetLocale();
63 const char * const cloc
= setlocale(LC_ALL
, NULL
);
64 if ( m_wxloc
|| m_cloc
)
66 if ( m_wxloc
== wxloc
&& strcmp(m_cloc
, cloc
) == 0 )
71 //else: Not initialized yet.
74 m_cloc
= wxCRT_StrdupA(cloc
);
89 // Non-owned pointer to wxLocale which was used.
93 // Owned pointer to the C locale string.
96 wxDECLARE_NO_COPY_CLASS(LocaleId
);
99 } // anonymous namespace
101 // ============================================================================
102 // wxNumberFormatter implementation
103 // ============================================================================
105 // ----------------------------------------------------------------------------
106 // Locale information accessors
107 // ----------------------------------------------------------------------------
109 wxChar
wxNumberFormatter::GetDecimalSeparator()
112 // Notice that while using static variable here is not MT-safe, the worst
113 // that can happen is that we redo the initialization if we're called
114 // concurrently from more than one thread so it's not a real problem.
115 static wxChar s_decimalSeparator
= 0;
117 // Remember the locale which was current when we initialized, we must redo
118 // the initialization if the locale changed.
119 static LocaleId s_localeUsedForInit
;
121 if ( s_localeUsedForInit
.NotInitializedOrHasChanged() )
124 s
= wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
);
127 // We really must have something for decimal separator, so fall
128 // back to the C locale default.
129 s_decimalSeparator
= '.';
133 // To the best of my knowledge there are no locales like this.
134 wxASSERT_MSG( s
.length() == 1,
135 "Multi-character decimal separator?" );
137 s_decimalSeparator
= s
[0];
141 return s_decimalSeparator
;
144 #endif // wxUSE_INTL/!wxUSE_INTL
147 bool wxNumberFormatter::GetThousandsSeparatorIfUsed(wxChar
*sep
)
150 static wxChar s_thousandsSeparator
= 0;
151 static LocaleId s_localeUsedForInit
;
153 if ( s_localeUsedForInit
.NotInitializedOrHasChanged() )
156 s
= wxLocale::GetInfo(wxLOCALE_THOUSANDS_SEP
, wxLOCALE_CAT_NUMBER
);
159 wxASSERT_MSG( s
.length() == 1,
160 "Multi-character thousands separator?" );
162 s_thousandsSeparator
= s
[0];
164 //else: Unlike above it's perfectly fine for the thousands separator to
165 // be empty if grouping is not used, so just leave it as 0.
168 if ( !s_thousandsSeparator
)
172 *sep
= s_thousandsSeparator
;
178 #endif // wxUSE_INTL/!wxUSE_INTL
181 // ----------------------------------------------------------------------------
182 // Conversion to string and helpers
183 // ----------------------------------------------------------------------------
185 wxString
wxNumberFormatter::PostProcessIntString(wxString s
, int style
)
187 if ( style
& Style_WithThousandsSep
)
188 AddThousandsSeparators(s
);
190 wxASSERT_MSG( !(style
& Style_NoTrailingZeroes
),
191 "Style_NoTrailingZeroes can't be used with integer values" );
196 wxString
wxNumberFormatter::ToString(long val
, int style
)
198 return PostProcessIntString(wxString::Format("%ld", val
), style
);
201 #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
203 wxString
wxNumberFormatter::ToString(wxLongLong_t val
, int style
)
205 return PostProcessIntString(wxString::Format("%" wxLongLongFmtSpec
"d", val
),
209 #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
211 wxString
wxNumberFormatter::ToString(double val
, int precision
, int style
)
213 wxString s
= wxString::FromDouble(val
,precision
);
215 if ( style
& Style_WithThousandsSep
)
216 AddThousandsSeparators(s
);
218 if ( style
& Style_NoTrailingZeroes
)
219 RemoveTrailingZeroes(s
);
224 void wxNumberFormatter::AddThousandsSeparators(wxString
& s
)
227 if ( !GetThousandsSeparatorIfUsed(&thousandsSep
) )
230 size_t pos
= s
.find(GetDecimalSeparator());
231 if ( pos
== wxString::npos
)
233 // Start grouping at the end of an integer number.
237 // End grouping at the beginning of the digits -- there could be at a sign
238 // before their start.
239 const size_t start
= s
.find_first_of("0123456789");
241 // We currently group digits by 3 independently of the locale. This is not
242 // the right thing to do and we should use lconv::grouping (under POSIX)
243 // and GetLocaleInfo(LOCALE_SGROUPING) (under MSW) to get information about
244 // the correct grouping to use. This is something that needs to be done at
245 // wxLocale level first and then used here in the future (TODO).
246 const size_t GROUP_LEN
= 3;
248 while ( pos
> start
+ GROUP_LEN
)
251 s
.insert(pos
, thousandsSep
);
255 void wxNumberFormatter::RemoveTrailingZeroes(wxString
& s
)
257 const size_t posDecSep
= s
.find(GetDecimalSeparator());
258 wxCHECK_RET( posDecSep
!= wxString::npos
,
259 wxString::Format("No decimal separator in \"%s\"", s
) );
260 wxCHECK_RET( posDecSep
, "Can't start with decimal separator" );
262 // Find the last character to keep.
263 size_t posLastNonZero
= s
.find_last_not_of("0");
265 // If it's the decimal separator itself, don't keep it neither.
266 if ( posLastNonZero
== posDecSep
)
269 s
.erase(posLastNonZero
+ 1);
272 // ----------------------------------------------------------------------------
273 // Conversion from strings
274 // ----------------------------------------------------------------------------
276 void wxNumberFormatter::RemoveThousandsSeparators(wxString
& s
)
279 if ( !GetThousandsSeparatorIfUsed(&thousandsSep
) )
282 s
.Replace(wxString(thousandsSep
), wxString());
285 bool wxNumberFormatter::FromString(wxString s
, long *val
)
287 RemoveThousandsSeparators(s
);
288 return s
.ToLong(val
);
291 #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
293 bool wxNumberFormatter::FromString(wxString s
, wxLongLong_t
*val
)
295 RemoveThousandsSeparators(s
);
296 return s
.ToLongLong(val
);
299 #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
301 bool wxNumberFormatter::FromString(wxString s
, double *val
)
303 RemoveThousandsSeparators(s
);
304 return s
.ToDouble(val
);