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::PostProcessIntString(wxString s
, int style
)
99 if ( style
& Style_WithThousandsSep
)
100 AddThousandsSeparators(s
);
102 wxASSERT_MSG( !(style
& Style_NoTrailingZeroes
),
103 "Style_NoTrailingZeroes can't be used with integer values" );
108 wxString
wxNumberFormatter::ToString(long val
, int style
)
110 return PostProcessIntString(wxString::Format("%ld", val
), style
);
113 #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
115 wxString
wxNumberFormatter::ToString(wxLongLong_t val
, int style
)
117 return PostProcessIntString(wxString::Format("%" wxLongLongFmtSpec
"d", val
),
121 #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
123 wxString
wxNumberFormatter::ToString(double val
, int precision
, int style
)
125 const wxString fmt
= wxString::Format("%%.%df", precision
);
126 wxString s
= wxString::Format(fmt
, val
);
128 if ( style
& Style_WithThousandsSep
)
129 AddThousandsSeparators(s
);
131 if ( style
& Style_NoTrailingZeroes
)
132 RemoveTrailingZeroes(s
);
137 void wxNumberFormatter::AddThousandsSeparators(wxString
& s
)
140 if ( !GetThousandsSeparatorIfUsed(&thousandsSep
) )
143 size_t pos
= s
.find(GetDecimalSeparator());
144 if ( pos
== wxString::npos
)
146 // Start grouping at the end of an integer number.
150 // We currently group digits by 3 independently of the locale. This is not
151 // the right thing to do and we should use lconv::grouping (under POSIX)
152 // and GetLocaleInfo(LOCALE_SGROUPING) (under MSW) to get information about
153 // the correct grouping to use. This is something that needs to be done at
154 // wxLocale level first and then used here in the future (TODO).
155 const size_t GROUP_LEN
= 3;
157 while ( pos
> GROUP_LEN
)
160 s
.insert(pos
, thousandsSep
);
164 void wxNumberFormatter::RemoveTrailingZeroes(wxString
& s
)
166 const size_t posDecSep
= s
.find(GetDecimalSeparator());
167 wxCHECK_RET( posDecSep
!= wxString::npos
, "No decimal separator" );
168 wxCHECK_RET( posDecSep
, "Can't start with decimal separator" );
170 // Find the last character to keep.
171 size_t posLastNonZero
= s
.find_last_not_of("0");
173 // If it's the decimal separator itself, don't keep it neither.
174 if ( posLastNonZero
== posDecSep
)
177 s
.erase(posLastNonZero
+ 1);
180 // ----------------------------------------------------------------------------
181 // Conversion from strings
182 // ----------------------------------------------------------------------------
184 void wxNumberFormatter::RemoveThousandsSeparators(wxString
& s
)
187 if ( !GetThousandsSeparatorIfUsed(&thousandsSep
) )
190 s
.Replace(wxString(thousandsSep
), wxString());
193 bool wxNumberFormatter::FromString(wxString s
, long *val
)
195 RemoveThousandsSeparators(s
);
196 return s
.ToLong(val
);
199 #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
201 bool wxNumberFormatter::FromString(wxString s
, wxLongLong_t
*val
)
203 RemoveThousandsSeparators(s
);
204 return s
.ToLongLong(val
);
207 #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
209 bool wxNumberFormatter::FromString(wxString s
, double *val
)
211 RemoveThousandsSeparators(s
);
212 return s
.ToDouble(val
);