Audacity  2.2.2
Public Types | Static Public Member Functions | Static Protected Member Functions | Static Private Member Functions | Friends | List of all members
NumberFormatter Class Reference

#include <numformatter.h>

Public Types

enum  Style {
  Style_None = 0x00, Style_WithThousandsSep = 0x01, Style_NoTrailingZeroes = 0x02, Style_OneTrailingZero = 0x04,
  Style_TwoTrailingZeroes = 0x08, Style_ThreeTrailingZeroes = 0x10
}
 

Static Public Member Functions

static wxString ToString (long val, int style=Style_WithThousandsSep)
 
static wxString ToString (wxLongLong_t val, int style=Style_WithThousandsSep)
 
static wxString ToString (double val, int precision, int style=Style_WithThousandsSep)
 
static bool FromString (const wxString &s, long *val)
 
static bool FromString (const wxString &s, wxLongLong_t *val)
 
static bool FromString (const wxString &s, double *val)
 
static wxChar GetDecimalSeparator ()
 
static bool GetThousandsSeparatorIfUsed (wxChar *sep)
 

Static Protected Member Functions

static void RemoveTrailingZeroes (wxString &s, size_t retain=0)
 

Static Private Member Functions

static wxString PostProcessIntString (const wxString &s, int style)
 
static void AddThousandsSeparators (wxString &s)
 
static void RemoveThousandsSeparators (wxString &s)
 

Friends

class FloatingPointValidatorBase
 

Detailed Description

Definition at line 23 of file numformatter.h.

Member Enumeration Documentation

Enumerator
Style_None 
Style_WithThousandsSep 
Style_NoTrailingZeroes 
Style_OneTrailingZero 
Style_TwoTrailingZeroes 
Style_ThreeTrailingZeroes 

Definition at line 27 of file numformatter.h.

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  };

Member Function Documentation

void NumberFormatter::AddThousandsSeparators ( wxString &  s)
staticprivate

Definition at line 177 of file numformatter.cpp.

References GetDecimalSeparator(), and GetThousandsSeparatorIfUsed().

Referenced by PostProcessIntString(), and ToString().

178 {
179  wxChar thousandsSep;
180  if ( !GetThousandsSeparatorIfUsed(&thousandsSep) )
181  return;
182 
183  size_t pos = s.find(GetDecimalSeparator());
184  if ( pos == wxString::npos )
185  {
186  // Start grouping at the end of an integer number.
187  pos = s.length();
188  }
189 
190  // End grouping at the beginning of the digits -- there could be at a sign
191  // before their start.
192  const size_t start = s.find_first_of(wxT("0123456789"));
193 
194  // We currently group digits by 3 independently of the locale. This is not
195  // the right thing to do and we should use lconv::grouping (under POSIX)
196  // and GetLocaleInfo(LOCALE_SGROUPING) (under MSW) to get information about
197  // the correct grouping to use. This is something that needs to be done at
198  // wxLocale level first and then used here in the future (TODO).
199  const size_t GROUP_LEN = 3;
200 
201  while ( pos > start + GROUP_LEN )
202  {
203  pos -= GROUP_LEN;
204  s.insert(pos, thousandsSep);
205  }
206 }
static wxChar GetDecimalSeparator()
static bool GetThousandsSeparatorIfUsed(wxChar *sep)
bool NumberFormatter::FromString ( const wxString &  s,
long *  val 
)
static

Definition at line 241 of file numformatter.cpp.

References RemoveThousandsSeparators().

242 {
243  wxString s(sArg);
245  return s.ToLong(val);
246 }
static void RemoveThousandsSeparators(wxString &s)
bool NumberFormatter::FromString ( const wxString &  s,
wxLongLong_t *  val 
)
static

Definition at line 250 of file numformatter.cpp.

References RemoveThousandsSeparators().

251 {
252  wxString s(sArg);
253 
255  return s.ToLongLong(val);
256 }
static void RemoveThousandsSeparators(wxString &s)
bool NumberFormatter::FromString ( const wxString &  s,
double *  val 
)
static

Definition at line 260 of file numformatter.cpp.

References RemoveThousandsSeparators().

261 {
262  wxString s(sArg);
263 
265  return s.ToDouble(val);
266 }
static void RemoveThousandsSeparators(wxString &s)
wxChar NumberFormatter::GetDecimalSeparator ( )
static

Definition at line 53 of file numformatter.cpp.

Referenced by AddThousandsSeparators(), and RemoveTrailingZeroes().

54 {
55 #if wxUSE_INTL
56  struct lconv *info = localeconv();
57  wxString s = info ? wxString::FromUTF8(info->decimal_point) : wxT(".");
58  if (s.empty())
59  {
60  // We really must have something for decimal separator, so fall
61  // back to the C locale default.
62  s = wxT(".");
63  }
64 
65  return s[0];
66 #else // !wxUSE_INTL
67  return wxT('.');
68 #endif // wxUSE_INTL/!wxUSE_INTL
69 }
bool NumberFormatter::GetThousandsSeparatorIfUsed ( wxChar *  sep)
static

Definition at line 71 of file numformatter.cpp.

Referenced by AddThousandsSeparators(), and RemoveThousandsSeparators().

72 {
73 #if wxUSE_INTL
74  struct lconv *info = localeconv();
75  wxString s = info ? wxString::FromUTF8(info->thousands_sep) : wxT("");
76 
77  if (s.IsEmpty())
78  {
79  return false;
80  }
81 
82  *sep = s[0];
83  return true;
84 #else // !wxUSE_INTL
85  wxUnusedVar(sep);
86  return false;
87 #endif // wxUSE_INTL/!wxUSE_INTL
88 }
wxString NumberFormatter::PostProcessIntString ( const wxString &  s,
int  style 
)
staticprivate

Definition at line 94 of file numformatter.cpp.

References AddThousandsSeparators(), Style_NoTrailingZeroes, Style_OneTrailingZero, Style_ThreeTrailingZeroes, Style_TwoTrailingZeroes, and Style_WithThousandsSep.

Referenced by ToString().

95 {
96  wxString s(sArg);
97 
98  if ( style & Style_WithThousandsSep )
100 
101  wxASSERT_MSG( !(style & Style_NoTrailingZeroes),
102  wxT("Style_NoTrailingZeroes can't be used with integer values") );
103  wxASSERT_MSG( !(style & Style_OneTrailingZero),
104  wxT("Style_OneTrailingZero can't be used with integer values") );
105  wxASSERT_MSG( !(style & Style_TwoTrailingZeroes),
106  wxT("Style_TwoTrailingZeroes can't be used with integer values") );
107  wxASSERT_MSG( !(style & Style_ThreeTrailingZeroes),
108  wxT("Style_ThreeTrailingZeroes can't be used with integer values") );
109 
110  return s;
111 }
static void AddThousandsSeparators(wxString &s)
void NumberFormatter::RemoveThousandsSeparators ( wxString &  s)
staticprivate

Definition at line 232 of file numformatter.cpp.

References GetThousandsSeparatorIfUsed().

Referenced by FromString().

233 {
234  wxChar thousandsSep;
235  if ( !GetThousandsSeparatorIfUsed(&thousandsSep) )
236  return;
237 
238  s.Replace(wxString(thousandsSep), wxString());
239 }
static bool GetThousandsSeparatorIfUsed(wxChar *sep)
void NumberFormatter::RemoveTrailingZeroes ( wxString &  s,
size_t  retain = 0 
)
staticprotected

Definition at line 208 of file numformatter.cpp.

References GetDecimalSeparator().

Referenced by ToString().

209 {
210  const size_t posDecSep = s.find(GetDecimalSeparator());
211  wxCHECK_RET( posDecSep != wxString::npos,
212  wxString::Format(wxT("No decimal separator in \"%s\""), s) );
213  wxCHECK_RET( posDecSep, wxT("Can't start with decimal separator" ));
214 
215  // Find the last character to keep.
216  size_t posLastCharacterToKeep = s.find_last_not_of(wxT("0"));
217 
218  // If it's the decimal separator itself, remove it.
219  if ((posLastCharacterToKeep == posDecSep) && (retain == 0)) {
220  posLastCharacterToKeep--;
221  } else if ((posLastCharacterToKeep - posDecSep) < retain) {
222  posLastCharacterToKeep = retain + posDecSep;
223  }
224 
225  s.erase(posLastCharacterToKeep + 1);
226 }
static wxChar GetDecimalSeparator()
wxString NumberFormatter::ToString ( long  val,
int  style = Style_WithThousandsSep 
)
static

Definition at line 113 of file numformatter.cpp.

References PostProcessIntString().

114 {
115  return PostProcessIntString(wxString::Format(wxT("%ld"), val), style);
116 }
static wxString PostProcessIntString(const wxString &s, int style)
wxString NumberFormatter::ToString ( wxLongLong_t  val,
int  style = Style_WithThousandsSep 
)
static

Definition at line 120 of file numformatter.cpp.

References PostProcessIntString().

121 {
122  return PostProcessIntString(wxString::Format("%" wxLongLongFmtSpec "d", val),
123  style);
124 }
static wxString PostProcessIntString(const wxString &s, int style)
wxString NumberFormatter::ToString ( double  val,
int  precision,
int  style = Style_WithThousandsSep 
)
static

Definition at line 128 of file numformatter.cpp.

References _(), AddThousandsSeparators(), format, RemoveTrailingZeroes(), Style_NoTrailingZeroes, Style_OneTrailingZero, Style_ThreeTrailingZeroes, Style_TwoTrailingZeroes, and Style_WithThousandsSep.

129 {
130  wxString format;
131  if ( precision == -1 )
132  {
133  format = wxT("%g");
134  }
135  else // Use fixed precision.
136  {
137  format.Printf(wxT("%%.%df"), precision);
138  }
139 
140  if (std::isnan(val))
141  {
142  return _("NaN");
143  }
144  if (std::isinf(val))
145  {
146  if (val == std::numeric_limits<double>::infinity())
147  {
148  return _("Infinity");
149  }
150  else
151  {
152  return _("-Infinity");
153  }
154  }
155  wxString s = wxString::Format(format, val);
156 
157  if ( style & Style_WithThousandsSep )
159 
160  if ( precision != -1 )
161  {
162  if ( style & Style_NoTrailingZeroes )
163  RemoveTrailingZeroes(s, 0);
164 
165  if ( style & Style_OneTrailingZero )
166  RemoveTrailingZeroes(s, 1);
167 
168  if ( style & Style_TwoTrailingZeroes )
169  RemoveTrailingZeroes(s, 2);
170 
171  if ( style & Style_ThreeTrailingZeroes )
172  RemoveTrailingZeroes(s, 3);
173  }
174  return s;
175 }
static void RemoveTrailingZeroes(wxString &s, size_t retain=0)
int format
Definition: ExportPCM.cpp:56
_("Move Track &Down")+wxT("\t")+(GetActiveProject() -> GetCommandManager() ->GetKeyFromName(wxT("TrackMoveDown")).Raw()), OnMoveTrack) POPUP_MENU_ITEM(OnMoveTopID, _("Move Track to &Top")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveTop")).Raw()), OnMoveTrack) POPUP_MENU_ITEM(OnMoveBottomID, _("Move Track to &Bottom")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveBottom")).Raw()), OnMoveTrack)#define SET_TRACK_NAME_PLUGIN_SYMBOLclass SetTrackNameCommand:public AudacityCommand
static void AddThousandsSeparators(wxString &s)

Friends And Related Function Documentation

friend class FloatingPointValidatorBase
friend

Definition at line 87 of file numformatter.h.


The documentation for this class was generated from the following files: