Numbers
RWLocale also provides you with an interface for conversions between strings and numbers, both integers and floating point values. RWLocaleSnapshot and RWAnsiLocale implement this interface. RWLocaleSnapshot provides the full range of capabilities defined by the Standard C Library type struct lconv, while RWAnsiLocale provides this functionality through the numerics facets as defined by the C++ Standard. The capabilities include using appropriate digit group separators, decimal “point”, and currency notation. When converting from strings, both RWLocaleSnapshot and RWAnsiLocale allow and check the same digit group separators.
Unfortunately, stream operations of this class are clumsier than we might like, since the standard iostream library provides definitions for number insertion and extraction operators which cannot be overridden. Instead, we can use
RWCString functions directly:
RWLocaleSnapshot french("fr");
double f = 1234567.89;
long i = 987654;
RWCString fs = french.asString(f, 2);
RWCString is = french.asString(i);
if (french.stringToNum(fs, &f) &&
french.stringToNum(is, &i)) // verify conversion
cout << "C:\t" << f << "\t" << i << endl
<< "French:\t" << fs << "\t" << is << endl;
Since the French use periods for digit group separators, and commas to separate the integer from the fraction part of a number, this code might display as:
C: 1.234567e+07 987654
French: 1.234.567,89 987.654
You will notice that numbers with digit group separators are easier to read.
If you are using
RWDecimalPortable to parse decimals, it is recommended that you first explicitly define a locale, rather than allowing
RWLocale to provide a default locale.
RWDecimalPortable’s parsing behavior is locale-dependent, so may produce unexpected results. For example, the C locale does not use a comma as a thousands separator, and parsing would therefore recognize such a comma instead as a separator between groups of digits.
NOTE: It is recommended that you explicitly define a locale when using RWDecimalPortable, as the default locale may produce unexpected results.
To ensure predictable and expected behavior, the program should explicitly specify the locale before using the
RWDecimalPortable constructor, and input strings should be formatted according the numeric string facets for that locale.