numformatter.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/numformatter.h
  3. // Purpose: interface to wxNumberFormatter
  4. // Author: Fulvio Senore, Vadim Zeitlin
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @class wxNumberFormatter
  9. Helper class for formatting and parsing numbers with thousands separators.
  10. This class contains only static functions, so users must not create instances
  11. but directly call the member functions.
  12. @since 2.9.2
  13. @library{wxbase}
  14. */
  15. class wxNumberFormatter
  16. {
  17. public:
  18. /**
  19. Bit masks used with ToString().
  20. */
  21. enum Style
  22. {
  23. /**
  24. This flag can be used to indicate absence of any other flags below.
  25. */
  26. Style_None = 0x00,
  27. /**
  28. If this flag is given, thousands separators will be inserted in the
  29. number string representation as defined by the current locale.
  30. */
  31. Style_WithThousandsSep = 0x01,
  32. /**
  33. If this flag is given, trailing zeroes in a floating point number
  34. string representation will be omitted.
  35. If the number is actually integer, the decimal separator will be
  36. omitted as well. To give an example, formatting the number @c 1.23
  37. with precision 5 will normally yield "1.23000" but with this flag
  38. it would return "1.23". And formatting @c 123 with this flag will
  39. return just "123" for any precision.
  40. This flag can't be used with ToString() overload taking the integer
  41. value.
  42. */
  43. Style_NoTrailingZeroes = 0x02
  44. };
  45. /**
  46. Returns string representation of an integer number.
  47. By default, the string will use thousands separators if appropriate for
  48. the current locale. This can be avoided by passing Style_None as @a
  49. flags in which case the call to the function has exactly the same
  50. effect as <code>wxString::Format("%ld", val)</code>.
  51. Notice that calling ToString() with a value of type @c int and
  52. non-default flags results in ambiguity between this overload and the
  53. one below. To resolve it, you need to cast the value to @c long.
  54. @param val
  55. The variable to convert to a string.
  56. @param flags
  57. Combination of values from the Style enumeration (except for
  58. Style_NoTrailingZeroes which can't be used with this overload).
  59. */
  60. static wxString ToString(long val, int flags = Style_WithThousandsSep);
  61. /**
  62. Returns string representation of a floating point number.
  63. @param val
  64. The variable to convert to a string.
  65. @param precision
  66. Number of decimals to write in formatted string.
  67. @param flags
  68. Combination of values from the Style enumeration.
  69. */
  70. static wxString
  71. ToString(double val, int precision, int flags = Style_WithThousandsSep);
  72. /**
  73. Parse a string representation of a number possibly including thousands
  74. separators.
  75. These functions parse number representation in the current locale. On
  76. success they return @true and store the result at the location pointed
  77. to by @a val (which can't be @NULL), otherwise @false is returned.
  78. @see wxString::ToLong(), wxString::ToDouble()
  79. */
  80. //@{
  81. static bool FromString(wxString s, long *val);
  82. static bool FromString(wxString s, double *val);
  83. //@}
  84. /**
  85. Get the decimal separator for the current locale.
  86. Decimal separators is always defined and we fall back to returning '.'
  87. in case of an error.
  88. */
  89. static wxChar GetDecimalSeparator();
  90. /**
  91. Get the thousands separator if grouping of the digits is used by the
  92. current locale.
  93. The value returned in @a sep should be only used if the function
  94. returns @true, otherwise no thousands separator should be used at all.
  95. @param sep
  96. Points to the variable receiving the thousands separator character
  97. if it is used by the current locale. May be @NULL if only the
  98. function return value is needed.
  99. */
  100. static bool GetThousandsSeparatorIfUsed(wxChar *sep);
  101. };