numformatter.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/numformatter.h
  3. // Purpose: wxNumberFormatter class
  4. // Author: Fulvio Senore, Vadim Zeitlin
  5. // Created: 2010-11-06
  6. // Copyright: (c) 2010 wxWidgets team
  7. // Licence: wxWindows licence
  8. /////////////////////////////////////////////////////////////////////////////
  9. #ifndef _WX_NUMFORMATTER_H_
  10. #define _WX_NUMFORMATTER_H_
  11. #include "wx/string.h"
  12. // Helper class for formatting numbers with thousands separators which also
  13. // supports parsing the numbers formatted by it.
  14. class WXDLLIMPEXP_BASE wxNumberFormatter
  15. {
  16. public:
  17. // Bit masks for ToString()
  18. enum Style
  19. {
  20. Style_None = 0x00,
  21. Style_WithThousandsSep = 0x01,
  22. Style_NoTrailingZeroes = 0x02 // Only for floating point numbers
  23. };
  24. // Format a number as a string. By default, the thousands separator is
  25. // used, specify Style_None to prevent this. For floating point numbers,
  26. // precision can also be specified.
  27. static wxString ToString(long val,
  28. int style = Style_WithThousandsSep);
  29. #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
  30. static wxString ToString(wxLongLong_t val,
  31. int style = Style_WithThousandsSep);
  32. #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
  33. static wxString ToString(double val,
  34. int precision,
  35. int style = Style_WithThousandsSep);
  36. // Parse a string representing a number, possibly with thousands separator.
  37. //
  38. // Return true on success and stores the result in the provided location
  39. // which must be a valid non-NULL pointer.
  40. static bool FromString(wxString s, long *val);
  41. #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
  42. static bool FromString(wxString s, wxLongLong_t *val);
  43. #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
  44. static bool FromString(wxString s, double *val);
  45. // Get the decimal separator for the current locale. It is always defined
  46. // and we fall back to returning '.' in case of an error.
  47. static wxChar GetDecimalSeparator();
  48. // Get the thousands separator if grouping of the digits is used by the
  49. // current locale. The value returned in sep should be only used if the
  50. // function returns true.
  51. static bool GetThousandsSeparatorIfUsed(wxChar *sep);
  52. private:
  53. // Post-process the string representing an integer.
  54. static wxString PostProcessIntString(wxString s, int style);
  55. // Add the thousands separators to a string representing a number without
  56. // the separators. This is used by ToString(Style_WithThousandsSep).
  57. static void AddThousandsSeparators(wxString& s);
  58. // Remove trailing zeroes and, if there is nothing left after it, the
  59. // decimal separator itself from a string representing a floating point
  60. // number. Also used by ToString().
  61. static void RemoveTrailingZeroes(wxString& s);
  62. // Remove all thousands separators from a string representing a number.
  63. static void RemoveThousandsSeparators(wxString& s);
  64. };
  65. #endif // _WX_NUMFORMATTER_H_