]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: wx/numformatter.h | |
3 | // Purpose: wxNumberFormatter class | |
4 | // Author: Fulvio Senore, Vadim Zeitlin | |
5 | // Created: 2010-11-06 | |
6 | // Copyright: (c) 2010 wxWidgets team | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | #ifndef _WX_NUMFORMATTER_H_ | |
11 | #define _WX_NUMFORMATTER_H_ | |
12 | ||
13 | #include "wx/string.h" | |
14 | ||
15 | // Helper class for formatting numbers with thousands separators which also | |
16 | // supports parsing the numbers formatted by it. | |
17 | class WXDLLIMPEXP_BASE wxNumberFormatter | |
18 | { | |
19 | public: | |
20 | // Bit masks for ToString() | |
21 | enum Style | |
22 | { | |
23 | Style_None = 0x00, | |
24 | Style_WithThousandsSep = 0x01, | |
25 | Style_NoTrailingZeroes = 0x02 // Only for floating point numbers | |
26 | }; | |
27 | ||
28 | // Format a number as a string. By default, the thousands separator is | |
29 | // used, specify Style_None to prevent this. For floating point numbers, | |
30 | // precision can also be specified. | |
31 | static wxString ToString(long val, | |
32 | int style = Style_WithThousandsSep); | |
33 | #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG | |
34 | static wxString ToString(wxLongLong_t val, | |
35 | int style = Style_WithThousandsSep); | |
36 | #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG | |
37 | static wxString ToString(double val, | |
38 | int precision, | |
39 | int style = Style_WithThousandsSep); | |
40 | ||
41 | // Parse a string representing a number, possibly with thousands separator. | |
42 | // | |
43 | // Return true on success and stores the result in the provided location | |
44 | // which must be a valid non-NULL pointer. | |
45 | static bool FromString(wxString s, long *val); | |
46 | #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG | |
47 | static bool FromString(wxString s, wxLongLong_t *val); | |
48 | #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG | |
49 | static bool FromString(wxString s, double *val); | |
50 | ||
51 | ||
52 | // Get the decimal separator for the current locale. It is always defined | |
53 | // and we fall back to returning '.' in case of an error. | |
54 | static wxChar GetDecimalSeparator(); | |
55 | ||
56 | // Get the thousands separator if grouping of the digits is used by the | |
57 | // current locale. The value returned in sep should be only used if the | |
58 | // function returns true. | |
59 | static bool GetThousandsSeparatorIfUsed(wxChar *sep); | |
60 | ||
61 | private: | |
62 | // Post-process the string representing an integer. | |
63 | static wxString PostProcessIntString(wxString s, int style); | |
64 | ||
65 | // Add the thousands separators to a string representing a number without | |
66 | // the separators. This is used by ToString(Style_WithThousandsSep). | |
67 | static void AddThousandsSeparators(wxString& s); | |
68 | ||
69 | // Remove trailing zeroes and, if there is nothing left after it, the | |
70 | // decimal separator itself from a string representing a floating point | |
71 | // number. Also used by ToString(). | |
72 | static void RemoveTrailingZeroes(wxString& s); | |
73 | ||
74 | // Remove all thousands separators from a string representing a number. | |
75 | static void RemoveThousandsSeparators(wxString& s); | |
76 | }; | |
77 | ||
78 | #endif // _WX_NUMFORMATTER_H_ |