]> git.saurik.com Git - wxWidgets.git/blob - src/common/numformatter.cpp
851259cbfd6f9d7e3bc249ca8c089330fd60ed69
[wxWidgets.git] / src / common / numformatter.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: numformatter.cpp
3 // Purpose: wxNumberFormatter
4 // Author: Fulvio Senore, Vadim Zeitlin
5 // Created: 2010-11-06
6 // Copyright: (c) 2010 wxWidgets team
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
16
17 #ifdef __BORLANDC__
18 #pragma hdrstop
19 #endif
20
21 #include "wx/numformatter.h"
22 #include "wx/intl.h"
23
24 // ============================================================================
25 // wxNumberFormatter implementation
26 // ============================================================================
27
28 // ----------------------------------------------------------------------------
29 // Locale information accessors
30 // ----------------------------------------------------------------------------
31
32 wxChar wxNumberFormatter::GetDecimalSeparator()
33 {
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;
38
39 if ( !s_decimalSeparator )
40 {
41 const wxString
42 s = wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT, wxLOCALE_CAT_NUMBER);
43 if ( s.empty() )
44 {
45 // We really must have something for decimal separator, so fall
46 // back to the C locale default.
47 s_decimalSeparator = '.';
48 }
49 else
50 {
51 // To the best of my knowledge there are no locales like this.
52 wxASSERT_MSG( s.length() == 1,
53 "Multi-character decimal separator?" );
54
55 s_decimalSeparator = s[0];
56 }
57 }
58
59 return s_decimalSeparator;
60 }
61
62 bool wxNumberFormatter::GetThousandsSeparatorIfUsed(wxChar *sep)
63 {
64 static wxChar s_thousandsSeparator = 0;
65 static bool s_initialized = false;
66
67 if ( !s_initialized )
68 {
69 const wxString
70 s = wxLocale::GetInfo(wxLOCALE_THOUSANDS_SEP, wxLOCALE_CAT_NUMBER);
71 if ( !s.empty() )
72 {
73 wxASSERT_MSG( s.length() == 1,
74 "Multi-character thousands separator?" );
75
76 s_thousandsSeparator = s[0];
77 }
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.
80
81 s_initialized = true;
82 }
83
84 if ( !s_thousandsSeparator )
85 return false;
86
87 if ( sep )
88 *sep = s_thousandsSeparator;
89
90 return true;
91 }
92
93 // ----------------------------------------------------------------------------
94 // Conversion to string and helpers
95 // ----------------------------------------------------------------------------
96
97 wxString wxNumberFormatter::ToString(long val, int style)
98 {
99 wxString s = wxString::Format("%ld", val);
100
101 if ( style & Style_WithThousandsSep )
102 AddThousandsSeparators(s);
103
104 wxASSERT_MSG( !(style & Style_NoTrailingZeroes),
105 "Style_NoTrailingZeroes can't be used with integer values" );
106
107 return s;
108 }
109
110 wxString wxNumberFormatter::ToString(double val, int precision, int style)
111 {
112 const wxString fmt = wxString::Format("%%.%df", precision);
113 wxString s = wxString::Format(fmt, val);
114
115 if ( style & Style_WithThousandsSep )
116 AddThousandsSeparators(s);
117
118 if ( style & Style_NoTrailingZeroes )
119 RemoveTrailingZeroes(s);
120
121 return s;
122 }
123
124 void wxNumberFormatter::AddThousandsSeparators(wxString& s)
125 {
126 wxChar thousandsSep;
127 if ( !GetThousandsSeparatorIfUsed(&thousandsSep) )
128 return;
129
130 size_t pos = s.find(GetDecimalSeparator());
131 if ( pos == wxString::npos )
132 {
133 // Start grouping at the end of an integer number.
134 pos = s.length();
135 }
136
137 // We currently group digits by 3 independently of the locale. This is not
138 // the right thing to do and we should use lconv::grouping (under POSIX)
139 // and GetLocaleInfo(LOCALE_SGROUPING) (under MSW) to get information about
140 // the correct grouping to use. This is something that needs to be done at
141 // wxLocale level first and then used here in the future (TODO).
142 const size_t GROUP_LEN = 3;
143
144 while ( pos > GROUP_LEN )
145 {
146 pos -= GROUP_LEN;
147 s.insert(pos, thousandsSep);
148 }
149 }
150
151 void wxNumberFormatter::RemoveTrailingZeroes(wxString& s)
152 {
153 const size_t posDecSep = s.find(GetDecimalSeparator());
154 wxCHECK_RET( posDecSep != wxString::npos, "No decimal separator" );
155 wxCHECK_RET( posDecSep, "Can't start with decimal separator" );
156
157 // Find the last character to keep.
158 size_t posLastNonZero = s.find_last_not_of("0");
159
160 // If it's the decimal separator itself, don't keep it neither.
161 if ( posLastNonZero == posDecSep )
162 posLastNonZero--;
163
164 s.erase(posLastNonZero + 1);
165 }
166
167 // ----------------------------------------------------------------------------
168 // Conversion from strings
169 // ----------------------------------------------------------------------------
170
171 void wxNumberFormatter::RemoveThousandsSeparators(wxString& s)
172 {
173 wxChar thousandsSep;
174 if ( !GetThousandsSeparatorIfUsed(&thousandsSep) )
175 return;
176
177 s.Replace(wxString(thousandsSep), wxString());
178 }
179
180 bool wxNumberFormatter::FromString(wxString s, long *val)
181 {
182 RemoveThousandsSeparators(s);
183 return s.ToLong(val);
184 }
185
186 bool wxNumberFormatter::FromString(wxString s, double *val)
187 {
188 RemoveThousandsSeparators(s);
189 return s.ToDouble(val);
190 }