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