Description
The C++ standard library provides several mechanisms for parsing and formatting numeric values. The values may be integers, floating-point values, calendar dates and times, and monetary values. Different countries and cultures use different formats and conventions for these, and so it's important to be aware of the techniques for internationalizing programs.
num_get<T>::get() and num_put<T>::put() are used to read and write numbers from streams. They apply the current locale settings to parse and format numbers.
numpunct<T> is used to obtain number punctuation, for example the character used for the decimal point in a number, or the text value of the boolean constant true.
time_get<T> and time_put<T> are used to parse and format dates and times. They operate on internal struct tm * times, and parse and format according to specified date and time formats (such as %x for the local date representation).
moneypunct<T> is used to obtain information about how to punctuate monetary values (such as the number of fractional digits).
Finally, money_get<T> and money_put<T> are used to parse and format monetary values.
Concept
The sample program illustrates some of the concepts described above. Number parsing and formatting is done using istringstream and ostringstream for convenience.
First, the number 1234 is read from a string using num_get, and then it's written back out to another string using num_put. Then the local value for the decimal point and the text for the boolean constant true are retrieved and displayed.Then the current calendar date is obtained and formatted into an output string. This string is then parsed to extract the date.
Next, frac_digits() is called to obtain the number of fractional digits for currency formatting, and finally, money_get and money_put are used to actually parse and format a currency value. Output of the program is:
1234 decimal point = . boolean "true" name = true 05/14/98 05/14/98 frac dig = 2 12.34 12.34
Supported
Supported
Supported
