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 // ----------------------------------------------------------------------------
26 // ----------------------------------------------------------------------------
31 // Contains information about the locale which was used to initialize our
32 // cached values of the decimal and thousands separators. Notice that it isn't
33 // enough to store just wxLocale because the user code may call setlocale()
34 // directly and storing just C locale string is not enough because we can use
35 // the OS API directly instead of the CRT ones on some platforms. So just store
51 // Return true if this is the first time this function is called for this
52 // object or if the program locale has changed since the last time it was
53 // called. Otherwise just return false indicating that updating locale-
54 // dependent information is not necessary.
55 bool NotInitializedOrHasChanged()
57 wxLocale
* const wxloc
= wxGetLocale();
58 const char * const cloc
= setlocale(LC_ALL
, NULL
);
59 if ( m_wxloc
|| m_cloc
)
61 if ( m_wxloc
== wxloc
&& strcmp(m_cloc
, cloc
) == 0 )
66 //else: Not initialized yet.
69 m_cloc
= wxCRT_StrdupA(cloc
);
80 // Non-owned pointer to wxLocale which was used.
83 // Owned pointer to the C locale string.
86 wxDECLARE_NO_COPY_CLASS(LocaleId
);
89 } // anonymous namespace
91 // ============================================================================
92 // wxNumberFormatter implementation
93 // ============================================================================
95 // ----------------------------------------------------------------------------
96 // Locale information accessors
97 // ----------------------------------------------------------------------------
99 wxChar
wxNumberFormatter::GetDecimalSeparator()
101 // Notice that while using static variable here is not MT-safe, the worst
102 // that can happen is that we redo the initialization if we're called
103 // concurrently from more than one thread so it's not a real problem.
104 static wxChar s_decimalSeparator
= 0;
106 // Remember the locale which was current when we initialized, we must redo
107 // the initialization if the locale changed.
108 static LocaleId s_localeUsedForInit
;
110 if ( s_localeUsedForInit
.NotInitializedOrHasChanged() )
113 s
= wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
);
116 // We really must have something for decimal separator, so fall
117 // back to the C locale default.
118 s_decimalSeparator
= '.';
122 // To the best of my knowledge there are no locales like this.
123 wxASSERT_MSG( s
.length() == 1,
124 "Multi-character decimal separator?" );
126 s_decimalSeparator
= s
[0];
130 return s_decimalSeparator
;
133 bool wxNumberFormatter::GetThousandsSeparatorIfUsed(wxChar
*sep
)
135 static wxChar s_thousandsSeparator
= 0;
136 static LocaleId s_localeUsedForInit
;
138 if ( s_localeUsedForInit
.NotInitializedOrHasChanged() )
141 s
= wxLocale::GetInfo(wxLOCALE_THOUSANDS_SEP
, wxLOCALE_CAT_NUMBER
);
144 wxASSERT_MSG( s
.length() == 1,
145 "Multi-character thousands separator?" );
147 s_thousandsSeparator
= s
[0];
149 //else: Unlike above it's perfectly fine for the thousands separator to
150 // be empty if grouping is not used, so just leave it as 0.
153 if ( !s_thousandsSeparator
)
157 *sep
= s_thousandsSeparator
;
162 // ----------------------------------------------------------------------------
163 // Conversion to string and helpers
164 // ----------------------------------------------------------------------------
166 wxString
wxNumberFormatter::PostProcessIntString(wxString s
, int style
)
168 if ( style
& Style_WithThousandsSep
)
169 AddThousandsSeparators(s
);
171 wxASSERT_MSG( !(style
& Style_NoTrailingZeroes
),
172 "Style_NoTrailingZeroes can't be used with integer values" );
177 wxString
wxNumberFormatter::ToString(long val
, int style
)
179 return PostProcessIntString(wxString::Format("%ld", val
), style
);
182 #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
184 wxString
wxNumberFormatter::ToString(wxLongLong_t val
, int style
)
186 return PostProcessIntString(wxString::Format("%" wxLongLongFmtSpec
"d", val
),
190 #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
192 wxString
wxNumberFormatter::ToString(double val
, int precision
, int style
)
194 const wxString fmt
= wxString::Format("%%.%df", precision
);
195 wxString s
= wxString::Format(fmt
, val
);
197 if ( style
& Style_WithThousandsSep
)
198 AddThousandsSeparators(s
);
200 if ( style
& Style_NoTrailingZeroes
)
201 RemoveTrailingZeroes(s
);
206 void wxNumberFormatter::AddThousandsSeparators(wxString
& s
)
209 if ( !GetThousandsSeparatorIfUsed(&thousandsSep
) )
212 size_t pos
= s
.find(GetDecimalSeparator());
213 if ( pos
== wxString::npos
)
215 // Start grouping at the end of an integer number.
219 // We currently group digits by 3 independently of the locale. This is not
220 // the right thing to do and we should use lconv::grouping (under POSIX)
221 // and GetLocaleInfo(LOCALE_SGROUPING) (under MSW) to get information about
222 // the correct grouping to use. This is something that needs to be done at
223 // wxLocale level first and then used here in the future (TODO).
224 const size_t GROUP_LEN
= 3;
226 while ( pos
> GROUP_LEN
)
229 s
.insert(pos
, thousandsSep
);
233 void wxNumberFormatter::RemoveTrailingZeroes(wxString
& s
)
235 const size_t posDecSep
= s
.find(GetDecimalSeparator());
236 wxCHECK_RET( posDecSep
!= wxString::npos
,
237 wxString::Format("No decimal separator in \"%s\"", s
) );
238 wxCHECK_RET( posDecSep
, "Can't start with decimal separator" );
240 // Find the last character to keep.
241 size_t posLastNonZero
= s
.find_last_not_of("0");
243 // If it's the decimal separator itself, don't keep it neither.
244 if ( posLastNonZero
== posDecSep
)
247 s
.erase(posLastNonZero
+ 1);
250 // ----------------------------------------------------------------------------
251 // Conversion from strings
252 // ----------------------------------------------------------------------------
254 void wxNumberFormatter::RemoveThousandsSeparators(wxString
& s
)
257 if ( !GetThousandsSeparatorIfUsed(&thousandsSep
) )
260 s
.Replace(wxString(thousandsSep
), wxString());
263 bool wxNumberFormatter::FromString(wxString s
, long *val
)
265 RemoveThousandsSeparators(s
);
266 return s
.ToLong(val
);
269 #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
271 bool wxNumberFormatter::FromString(wxString s
, wxLongLong_t
*val
)
273 RemoveThousandsSeparators(s
);
274 return s
.ToLongLong(val
);
277 #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
279 bool wxNumberFormatter::FromString(wxString s
, double *val
)
281 RemoveThousandsSeparators(s
);
282 return s
.ToDouble(val
);