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 // ----------------------------------------------------------------------------
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
54 // Return true if this is the first time this function is called for this
55 // object or if the program locale has changed since the last time it was
56 // called. Otherwise just return false indicating that updating locale-
57 // dependent information is not necessary.
58 bool NotInitializedOrHasChanged()
60 wxLocale
* const wxloc
= wxGetLocale();
61 const char * const cloc
= setlocale(LC_ALL
, NULL
);
62 if ( m_wxloc
|| m_cloc
)
64 if ( m_wxloc
== wxloc
&& strcmp(m_cloc
, cloc
) == 0 )
69 //else: Not initialized yet.
72 m_cloc
= wxCRT_StrdupA(cloc
);
87 // Non-owned pointer to wxLocale which was used.
91 // Owned pointer to the C locale string.
94 wxDECLARE_NO_COPY_CLASS(LocaleId
);
97 } // anonymous namespace
99 // ============================================================================
100 // wxNumberFormatter implementation
101 // ============================================================================
103 // ----------------------------------------------------------------------------
104 // Locale information accessors
105 // ----------------------------------------------------------------------------
107 wxChar
wxNumberFormatter::GetDecimalSeparator()
110 // Notice that while using static variable here is not MT-safe, the worst
111 // that can happen is that we redo the initialization if we're called
112 // concurrently from more than one thread so it's not a real problem.
113 static wxChar s_decimalSeparator
= 0;
115 // Remember the locale which was current when we initialized, we must redo
116 // the initialization if the locale changed.
117 static LocaleId s_localeUsedForInit
;
119 if ( s_localeUsedForInit
.NotInitializedOrHasChanged() )
122 s
= wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
);
125 // We really must have something for decimal separator, so fall
126 // back to the C locale default.
127 s_decimalSeparator
= '.';
131 // To the best of my knowledge there are no locales like this.
132 wxASSERT_MSG( s
.length() == 1,
133 "Multi-character decimal separator?" );
135 s_decimalSeparator
= s
[0];
139 return s_decimalSeparator
;
142 #endif // wxUSE_INTL/!wxUSE_INTL
145 bool wxNumberFormatter::GetThousandsSeparatorIfUsed(wxChar
*sep
)
148 static wxChar s_thousandsSeparator
= 0;
149 static LocaleId s_localeUsedForInit
;
151 if ( s_localeUsedForInit
.NotInitializedOrHasChanged() )
154 s
= wxLocale::GetInfo(wxLOCALE_THOUSANDS_SEP
, wxLOCALE_CAT_NUMBER
);
157 wxASSERT_MSG( s
.length() == 1,
158 "Multi-character thousands separator?" );
160 s_thousandsSeparator
= s
[0];
162 //else: Unlike above it's perfectly fine for the thousands separator to
163 // be empty if grouping is not used, so just leave it as 0.
166 if ( !s_thousandsSeparator
)
170 *sep
= s_thousandsSeparator
;
176 #endif // wxUSE_INTL/!wxUSE_INTL
179 // ----------------------------------------------------------------------------
180 // Conversion to string and helpers
181 // ----------------------------------------------------------------------------
183 wxString
wxNumberFormatter::PostProcessIntString(wxString s
, int style
)
185 if ( style
& Style_WithThousandsSep
)
186 AddThousandsSeparators(s
);
188 wxASSERT_MSG( !(style
& Style_NoTrailingZeroes
),
189 "Style_NoTrailingZeroes can't be used with integer values" );
194 wxString
wxNumberFormatter::ToString(long val
, int style
)
196 return PostProcessIntString(wxString::Format("%ld", val
), style
);
199 #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
201 wxString
wxNumberFormatter::ToString(wxLongLong_t val
, int style
)
203 return PostProcessIntString(wxString::Format("%" wxLongLongFmtSpec
"d", val
),
207 #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
209 wxString
wxNumberFormatter::ToString(double val
, int precision
, int style
)
211 const wxString fmt
= wxString::Format("%%.%df", precision
);
212 wxString s
= wxString::Format(fmt
, val
);
214 if ( style
& Style_WithThousandsSep
)
215 AddThousandsSeparators(s
);
217 if ( style
& Style_NoTrailingZeroes
)
218 RemoveTrailingZeroes(s
);
223 void wxNumberFormatter::AddThousandsSeparators(wxString
& s
)
226 if ( !GetThousandsSeparatorIfUsed(&thousandsSep
) )
229 size_t pos
= s
.find(GetDecimalSeparator());
230 if ( pos
== wxString::npos
)
232 // Start grouping at the end of an integer number.
236 // We currently group digits by 3 independently of the locale. This is not
237 // the right thing to do and we should use lconv::grouping (under POSIX)
238 // and GetLocaleInfo(LOCALE_SGROUPING) (under MSW) to get information about
239 // the correct grouping to use. This is something that needs to be done at
240 // wxLocale level first and then used here in the future (TODO).
241 const size_t GROUP_LEN
= 3;
243 while ( pos
> GROUP_LEN
)
246 s
.insert(pos
, thousandsSep
);
250 void wxNumberFormatter::RemoveTrailingZeroes(wxString
& s
)
252 const size_t posDecSep
= s
.find(GetDecimalSeparator());
253 wxCHECK_RET( posDecSep
!= wxString::npos
,
254 wxString::Format("No decimal separator in \"%s\"", s
) );
255 wxCHECK_RET( posDecSep
, "Can't start with decimal separator" );
257 // Find the last character to keep.
258 size_t posLastNonZero
= s
.find_last_not_of("0");
260 // If it's the decimal separator itself, don't keep it neither.
261 if ( posLastNonZero
== posDecSep
)
264 s
.erase(posLastNonZero
+ 1);
267 // ----------------------------------------------------------------------------
268 // Conversion from strings
269 // ----------------------------------------------------------------------------
271 void wxNumberFormatter::RemoveThousandsSeparators(wxString
& s
)
274 if ( !GetThousandsSeparatorIfUsed(&thousandsSep
) )
277 s
.Replace(wxString(thousandsSep
), wxString());
280 bool wxNumberFormatter::FromString(wxString s
, long *val
)
282 RemoveThousandsSeparators(s
);
283 return s
.ToLong(val
);
286 #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
288 bool wxNumberFormatter::FromString(wxString s
, wxLongLong_t
*val
)
290 RemoveThousandsSeparators(s
);
291 return s
.ToLongLong(val
);
294 #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
296 bool wxNumberFormatter::FromString(wxString s
, double *val
)
298 RemoveThousandsSeparators(s
);
299 return s
.ToDouble(val
);