1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/numformatter.h
3 // Purpose: wxNumberFormatter class
4 // Author: Fulvio Senore, Vadim Zeitlin
6 // Copyright: (c) 2010 wxWidgets team
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #ifndef _WX_NUMFORMATTER_H_
11 #define _WX_NUMFORMATTER_H_
13 #include "wx/string.h"
15 // Helper class for formatting numbers with thousands separators which also
16 // supports parsing the numbers formatted by it.
17 class WXDLLIMPEXP_BASE wxNumberFormatter
20 // Bit masks for ToString()
24 Style_WithThousandsSep
= 0x01,
25 Style_NoTrailingZeroes
= 0x02 // Only for floating point numbers
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
);
34 static wxString
ToString(double val
,
36 int style
= Style_WithThousandsSep
);
38 // Parse a string representing a number, possibly with thousands separator.
40 // Return true on success and stores the result in the provided location
41 // which must be a valid non-NULL pointer.
42 static bool FromString(wxString s
, long *val
);
43 static bool FromString(wxString s
, double *val
);
46 // Get the decimal separator for the current locale. It is always defined
47 // and we fall back to returning '.' in case of an error.
48 static wxChar
GetDecimalSeparator();
50 // Get the thousands separator if grouping of the digits is used by the
51 // current locale. The value returned in sep should be only used if the
52 // function returns true.
53 static bool GetThousandsSeparatorIfUsed(wxChar
*sep
);
56 // Add the thousands separators to a string representing a number without
57 // the separators. This is used by ToString(Style_WithThousandsSep).
58 static void AddThousandsSeparators(wxString
& s
);
60 // Remove trailing zeroes and, if there is nothing left after it, the
61 // decimal separator itself from a string representing a floating point
62 // number. Also used by ToString().
63 static void RemoveTrailingZeroes(wxString
& s
);
65 // Remove all thousands separators from a string representing a number.
66 static void RemoveThousandsSeparators(wxString
& s
);
69 #endif // _WX_NUMFORMATTER_H_