Audacity  3.0.3
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 <cstddef> // for size_t
18 #include <wx/chartype.h> // for typedef wxChar
19 #include <wx/longlong.h>
20 
21 class wxString;
22 
23 #define HAS_LONG_LONG_T_DIFFERENT_FROM_LONG
24 
25 // Helper class for formatting numbers with thousands separators which also
26 // supports parsing the numbers formatted by it.
28 {
29 public:
30  // Bit masks for ToString()
31  enum Style
32  {
33  Style_None = 0x00,
35  Style_NoTrailingZeroes = 0x02, // Only for floating point numbers
36  Style_OneTrailingZero = 0x04, // Only for floating point numbers
37  Style_TwoTrailingZeroes = 0x08, // Only for floating point numbers
38  Style_ThreeTrailingZeroes = 0x10 // Only for floating point numbers
39  };
40 
41  // Format a number as a string. By default, the thousands separator is
42  // used, specify Style_None to prevent this. For floating point numbers,
43  // precision can also be specified.
44  static wxString ToString(long val,
45  int style = Style_WithThousandsSep);
46 #ifdef HAS_LONG_LONG_T_DIFFERENT_FROM_LONG
47  static wxString ToString(wxLongLong_t val,
48  int style = Style_WithThousandsSep);
49 #endif // HAS_LONG_LONG_T_DIFFERENT_FROM_LONG
50  static wxString ToString(double val,
51  int precision,
52  int style = Style_WithThousandsSep);
53 
54  // Parse a string representing a number, possibly with thousands separator.
55  //
56  // Return true on success and stores the result in the provided location
57  // which must be a valid non-NULL pointer.
58  static bool FromString(const wxString &s, long *val);
59 #ifdef HAS_LONG_LONG_T_DIFFERENT_FROM_LONG
60  static bool FromString(const wxString &s, wxLongLong_t *val);
61 #endif // HAS_LONG_LONG_T_DIFFERENT_FROM_LONG
62  static bool FromString(const wxString &s, double *val);
63 
64 
65  // Get the decimal separator for the current locale. It is always defined
66  // and we fall back to returning '.' in case of an error.
67  static wxChar GetDecimalSeparator();
68 
69  // Get the thousands separator if grouping of the digits is used by the
70  // current locale. The value returned in sep should be only used if the
71  // function returns true.
72  static bool GetThousandsSeparatorIfUsed(wxChar *sep);
73 
74 private:
75  // Post-process the string representing an integer.
76  static wxString PostProcessIntString(const wxString &s, int style);
77 
78  // Add the thousands separators to a string representing a number without
79  // the separators. This is used by ToString(Style_WithThousandsSep).
80  static void AddThousandsSeparators(wxString& s);
81 
82  // Remove all thousands separators from a string representing a number.
83  static void RemoveThousandsSeparators(wxString& s);
84 
85 protected:
86  // Remove trailing zeroes and, if there is nothing left after it, the
87  // decimal separator itself from a string representing a floating point
88  // number. Also used by ToString().
89  static void RemoveTrailingZeroes(wxString& s, size_t retain = 0);
90 
92 };
93 
94 #endif // _WIDGETS_NUMFORMATTER_H_
NumberFormatter::AddThousandsSeparators
static void AddThousandsSeparators(wxString &s)
Definition: numformatter.cpp:180
NumberFormatter::PostProcessIntString
static wxString PostProcessIntString(const wxString &s, int style)
Definition: numformatter.cpp:97
NumberFormatter::FloatingPointValidatorBase
friend class FloatingPointValidatorBase
Definition: numformatter.h:91
NumberFormatter::FromString
static bool FromString(const wxString &s, long *val)
Definition: numformatter.cpp:244
NumberFormatter::GetDecimalSeparator
static wxChar GetDecimalSeparator()
Definition: numformatter.cpp:56
NumberFormatter::Style_OneTrailingZero
@ Style_OneTrailingZero
Definition: numformatter.h:36
NumberFormatter::RemoveThousandsSeparators
static void RemoveThousandsSeparators(wxString &s)
Definition: numformatter.cpp:235
NumberFormatter::ToString
static wxString ToString(long val, int style=Style_WithThousandsSep)
Definition: numformatter.cpp:116
NumberFormatter::Style_NoTrailingZeroes
@ Style_NoTrailingZeroes
Definition: numformatter.h:35
NumberFormatter::Style_None
@ Style_None
Definition: numformatter.h:33
NumberFormatter::RemoveTrailingZeroes
static void RemoveTrailingZeroes(wxString &s, size_t retain=0)
Definition: numformatter.cpp:211
NumberFormatter
Definition: numformatter.h:28
NumberFormatter::Style_ThreeTrailingZeroes
@ Style_ThreeTrailingZeroes
Definition: numformatter.h:38
NumberFormatter::Style
Style
Definition: numformatter.h:32
NumberFormatter::Style_TwoTrailingZeroes
@ Style_TwoTrailingZeroes
Definition: numformatter.h:37
NumberFormatter::Style_WithThousandsSep
@ Style_WithThousandsSep
Definition: numformatter.h:34
NumberFormatter::GetThousandsSeparatorIfUsed
static bool GetThousandsSeparatorIfUsed(wxChar *sep)
Definition: numformatter.cpp:74