#include <wx/numformatter.h>
Formatting and parsing numbers using the current UI locale conventions, including support for using the correct decimal point character and thousands separators.
This class contains only static functions, so users must not create instances but directly call the member functions.
Public Types | |
| enum | Style { Style_None = 0x00 , Style_WithThousandsSep = 0x01 , Style_NoTrailingZeroes = 0x02 , Style_SignPlus = 0x04 , Style_SignSpace = 0x08 , Style_Currency = 0x10 , Style_CurrencySymbol = 0x20 , Style_CurrencyCode = 0x40 } |
| Bit masks used with ToString(). More... | |
Static Public Member Functions | |
| static wxString | ToString (double val, int precision, int flags=Style_WithThousandsSep) |
| Returns string representation of a floating point number. | |
| static wxString | Format (const wxString &format, double val) |
| Format the given number using one of the floating point formats and ensure that the result uses the correct decimal separator. | |
| static wxChar | GetDecimalSeparator () |
| Get the decimal separator for the current UI locale. | |
| static bool | GetThousandsSeparatorIfUsed (wxChar *sep) |
| Get the thousands separator if grouping of the digits is used by the current UI locale. | |
| static void | RemoveTrailingZeroes (wxString &str) |
| Remove trailing zeroes and, if there is nothing left after it, the decimal separator itself from a string representing a floating point number. | |
| static wxString | RemoveCurrencySymbolOrCode (wxString str, int flags) |
| Remove currency symbol or code, and grouping separators, according to the given style flags. | |
| static wxString | ToString (long val, int flags=Style_WithThousandsSep) |
| Returns string representation of an integer number. | |
| static wxString | ToString (long long val, int flags=Style_WithThousandsSep) |
| Returns string representation of an integer number. | |
| static wxString | ToString (unsigned long long val, int flags=Style_WithThousandsSep) |
| Returns string representation of an integer number. | |
| static bool | FromString (wxString s, long *val) |
| Parse a string representation of a number possibly including thousands separators. | |
| static bool | FromString (wxString s, long long *val) |
| Parse a string representation of a number possibly including thousands separators. | |
| static bool | FromString (wxString s, unsigned long long *val) |
| Parse a string representation of a number possibly including thousands separators. | |
| static bool | FromString (wxString s, double *val) |
| Parse a string representation of a number possibly including thousands separators. | |
Bit masks used with ToString().
| Enumerator | |
|---|---|
| Style_None | This flag can be used to indicate absence of any other flags below. |
| Style_WithThousandsSep | If this flag is given, thousands separators will be inserted in the number string representation as defined by the current UI locale. Thousands separators will be applied according to the grouping rules of the current UI locale. |
| Style_NoTrailingZeroes | If this flag is given, trailing zeroes in a floating point number string representation will be omitted. If the number is actually integer, the decimal separator will be omitted as well. To give an example, formatting the number This flag can't be used with ToString() overload taking the integer value. |
| Style_SignPlus | If this flag is given, a + will prefix the number string representation of non-negative numbers. This style cannot be used with Style_SignSpace.
|
| Style_SignSpace | If this flag is given, a space will prefix the number string representation of non-negative numbers. This style cannot be used with Style_SignPlus.
|
| Style_Currency | If this flag is given, the number will be interpreted as a currency value and formatted according to the rules of the current UI locale.
|
| Style_CurrencySymbol | If this flag is given, the currency symbol of the current UI locale will be prepended or appended to the currency value according to the rules of the current locale. The style will only take effect, if it is combined with Style_Currency. The style should not be combined with Style_CurrencyCode.
|
| Style_CurrencyCode | If this flag is given, the ISO 42127 currency code of the current UI locale will be prepended to the currency value. It will be separated from the currency value by a space character. The style will only take effect, if it is combined with Style_Currency. The style should not be combined with Style_CurrencySymbol.
|
Format the given number using one of the floating point formats and ensure that the result uses the correct decimal separator.
Prefer using ToString() if possible, i.e. if format is "%g" or "%.Nf" which are supported by it directly.
|
static |
Parse a string representation of a number possibly including thousands separators.
These functions parse number representation in the current UI locale. On success they return true and store the result at the location pointed to by val (which can't be nullptr), otherwise false is returned.
Note that the overload taking unsigned long long value is only available since wxWidgets 3.1.5. Also, unlike wxString::ToULongLong() and the standard functions such as strtoul(), this overload does not accept, i.e. returns false, for the strings starting with the minus sign.
|
static |
Parse a string representation of a number possibly including thousands separators.
These functions parse number representation in the current UI locale. On success they return true and store the result at the location pointed to by val (which can't be nullptr), otherwise false is returned.
Note that the overload taking unsigned long long value is only available since wxWidgets 3.1.5. Also, unlike wxString::ToULongLong() and the standard functions such as strtoul(), this overload does not accept, i.e. returns false, for the strings starting with the minus sign.
|
static |
Parse a string representation of a number possibly including thousands separators.
These functions parse number representation in the current UI locale. On success they return true and store the result at the location pointed to by val (which can't be nullptr), otherwise false is returned.
Note that the overload taking unsigned long long value is only available since wxWidgets 3.1.5. Also, unlike wxString::ToULongLong() and the standard functions such as strtoul(), this overload does not accept, i.e. returns false, for the strings starting with the minus sign.
|
static |
Parse a string representation of a number possibly including thousands separators.
These functions parse number representation in the current UI locale. On success they return true and store the result at the location pointed to by val (which can't be nullptr), otherwise false is returned.
Note that the overload taking unsigned long long value is only available since wxWidgets 3.1.5. Also, unlike wxString::ToULongLong() and the standard functions such as strtoul(), this overload does not accept, i.e. returns false, for the strings starting with the minus sign.
|
static |
Get the decimal separator for the current UI locale.
Decimal separators is always defined and we fall back to returning '.' in case of an error.
|
static |
Get the thousands separator if grouping of the digits is used by the current UI locale.
The value returned in sep should be only used if the function returns true, otherwise no thousands separator should be used at all.
| sep | Points to the variable receiving the thousands separator character if it is used by the current UI locale. May be nullptr if only the function return value is needed. |
Remove currency symbol or code, and grouping separators, according to the given style flags.
| [in] | str | The string to remove the currency symbol or code or grouping separators from. |
| flags | Combination of values from the Style enumeration. |
|
static |
Remove trailing zeroes and, if there is nothing left after it, the decimal separator itself from a string representing a floating point number.
| [in,out] | str | The string to remove zeroes from. |
|
static |
Returns string representation of a floating point number.
| val | The variable to convert to a string. |
| precision | Number of decimals to write in formatted string. |
| flags | Combination of values from the Style enumeration. |
|
static |
Returns string representation of an integer number.
By default, the string will use thousands separators if appropriate for the current UI locale. This can be avoided by passing Style_None as flags in which case the call to the function has exactly the same effect as wxString::Format("%ld", val).
Notice that calling ToString() with a value of type int and non-default flags results in ambiguity between this overload and the one below. To resolve it, you need to cast the value to long.
| val | The variable to convert to a string. |
| flags | Combination of values from the Style enumeration (except for Style_NoTrailingZeroes which can't be used with this overload). |
|
static |
Returns string representation of an integer number.
By default, the string will use thousands separators if appropriate for the current UI locale. This can be avoided by passing Style_None as flags in which case the call to the function has exactly the same effect as wxString::Format("%ld", val).
Notice that calling ToString() with a value of type int and non-default flags results in ambiguity between this overload and the one below. To resolve it, you need to cast the value to long.
| val | The variable to convert to a string. |
| flags | Combination of values from the Style enumeration (except for Style_NoTrailingZeroes which can't be used with this overload). |
|
static |
Returns string representation of an integer number.
By default, the string will use thousands separators if appropriate for the current UI locale. This can be avoided by passing Style_None as flags in which case the call to the function has exactly the same effect as wxString::Format("%ld", val).
Notice that calling ToString() with a value of type int and non-default flags results in ambiguity between this overload and the one below. To resolve it, you need to cast the value to long.
| val | The variable to convert to a string. |
| flags | Combination of values from the Style enumeration (except for Style_NoTrailingZeroes which can't be used with this overload). |