Audacity 3.2.0
numformatter.h
Go to the documentation of this file.
1
2//
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
21class 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{
29public:
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,
46#ifdef HAS_LONG_LONG_T_DIFFERENT_FROM_LONG
47 static wxString ToString(wxLongLong_t val,
49#endif // HAS_LONG_LONG_T_DIFFERENT_FROM_LONG
50 static wxString ToString(double val,
51 int precision,
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
74private:
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
85protected:
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_
static bool FromString(const wxString &s, long *val)
static wxChar GetDecimalSeparator()
static wxString PostProcessIntString(const wxString &s, int style)
static void RemoveTrailingZeroes(wxString &s, size_t retain=0)
friend class FloatingPointValidatorBase
Definition: numformatter.h:91
static bool GetThousandsSeparatorIfUsed(wxChar *sep)
static void RemoveThousandsSeparators(wxString &s)
static void AddThousandsSeparators(wxString &s)
static wxString ToString(long val, int style=Style_WithThousandsSep)