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 const wxString fmt
= wxString::Format("%%.%df", precision
);
214 wxString s
= wxString::Format(fmt
, val
);
216 if ( style
& Style_WithThousandsSep
)
217 AddThousandsSeparators(s
);
219 if ( style
& Style_NoTrailingZeroes
)
220 RemoveTrailingZeroes(s
);
225 void wxNumberFormatter::AddThousandsSeparators(wxString
& s
)
228 if ( !GetThousandsSeparatorIfUsed(&thousandsSep
) )
231 size_t pos
= s
.find(GetDecimalSeparator());
232 if ( pos
== wxString::npos
)
234 // Start grouping at the end of an integer number.
238 // We currently group digits by 3 independently of the locale. This is not
239 // the right thing to do and we should use lconv::grouping (under POSIX)
240 // and GetLocaleInfo(LOCALE_SGROUPING) (under MSW) to get information about
241 // the correct grouping to use. This is something that needs to be done at
242 // wxLocale level first and then used here in the future (TODO).
243 const size_t GROUP_LEN
= 3;
245 while ( pos
> GROUP_LEN
)
248 s
.insert(pos
, thousandsSep
);
252 void wxNumberFormatter::RemoveTrailingZeroes(wxString
& s
)
254 const size_t posDecSep
= s
.find(GetDecimalSeparator());
255 wxCHECK_RET( posDecSep
!= wxString::npos
,
256 wxString::Format("No decimal separator in \"%s\"", s
) );
257 wxCHECK_RET( posDecSep
, "Can't start with decimal separator" );
259 // Find the last character to keep.
260 size_t posLastNonZero
= s
.find_last_not_of("0");
262 // If it's the decimal separator itself, don't keep it neither.
263 if ( posLastNonZero
== posDecSep
)
266 s
.erase(posLastNonZero
+ 1);
269 // ----------------------------------------------------------------------------
270 // Conversion from strings
271 // ----------------------------------------------------------------------------
273 void wxNumberFormatter::RemoveThousandsSeparators(wxString
& s
)
276 if ( !GetThousandsSeparatorIfUsed(&thousandsSep
) )
279 s
.Replace(wxString(thousandsSep
), wxString());
282 bool wxNumberFormatter::FromString(wxString s
, long *val
)
284 RemoveThousandsSeparators(s
);
285 return s
.ToLong(val
);
288 #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
290 bool wxNumberFormatter::FromString(wxString s
, wxLongLong_t
*val
)
292 RemoveThousandsSeparators(s
);
293 return s
.ToLongLong(val
);
296 #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
298 bool wxNumberFormatter::FromString(wxString s
, double *val
)
300 RemoveThousandsSeparators(s
);
301 return s
.ToDouble(val
);