Template Class Numpunct_byname

The facet numpunct specifies the punctuation used for parsing and formatting numeric quantities. You can specify the decimal point character, thousands separator, the grouping, and the spelling of true and false. If you construct numpunct_byname with a const char* that refers to a file, then that file is scanned by numpunct_byname's constructor for information to customize the encoding.

  numpunct_byname<char> np("en_US");
  

If the file "en_US" exists, has numpunct data in it, and there are no syntax errors in the data, then np will behave as dictated by that data. If the file exists, but does not have numpunct data in it, then the facet will behave as if it were constructed with "C". If the file has numpunct data in it, but there is a syntax error in the data, or if the file does not exist, then a std::runtime_error is thrown.

For numpunct_byname<char>, the numpunct data section begins with:

  $numeric_narrow
  

For numpunct_byname<wchar_t>, the numpunct data section begins with:

  $numeric_wide
  

The syntax for both the narrow and wide data sections is the same. There are keywords that allow you to specify the different parts of the numpunct data:

You enter data with one of these keywords, followed by an equal sign '=', and then the data. You can specify any or all of the keywords. Data not specified will default to that of the "C" locale. The first two keywords (decimal_point and thousands_sep) have character data associated with them. See the rules for Character Syntax for details. The last three keywords have string data associated with them. See the rules for String Syntax .

Listing: Example usage of numpunct_byname
$numeric_narrow
decimal_point = ','

thousands_sep = '.'

grouping = 3|2

false_name = nope

true_name = sure

Here is an example program using the above data for narrow streams:

  #include <sstream>
  #include <locale>
  #include <iostream>
  int main()
  {
     std::locale loc("my_loc");
     std::cout.imbue(loc);
     std::istringstream in("1.23.456 nope 1.23.456,789");
     in.imbue(loc);
     in >> std::boolalpha;
     long i;
     bool b;
     double d;
     in >> i >> b >> d;
     std::cout << i << '
        << std::boolalpha << !b << '
        << std::fixed << d;
  }
  

The output is:

  1.23.456
   sure
  1.23.456,789000