Library: Localization
numpunct_byname numpunct locale::facet
char_type decimal_point() do_decimal_point() do_falsename() |
do_grouping() do_thousands_sep() do_truename() falsename() |
grouping() numpunct() numpunct_byname() string_type |
thousands_sep() truename() |
A numeric punctuation facet used by the num_get and num_get facets for formatting and parsing.
#include <locale> namespace std { template <class charT> class numpunct; template <class charT> class numpunct_byname; }
template <> class numpunct<char>; template <> class numpunct<wchar_t>; template <> class numpunct_byname<char>; template <> class numpunct_byname<wchar_t>;
The numpunct facet specifies numeric punctuation associated with the C locale. The numpunct_byname facet is used with named locales.
Both the num_put and num_get facets make use of this facet.
namespace std { template <class charT> class numpunct : public locale::facet { public: typedef charT char_type; typedef basic_string<charT> string_type; explicit numpunct(size_t refs = 0); char_type decimal_point() const; char_type thousands_sep() const; string grouping() const; string_type truename() const; string_type falsename() const; static locale::id id; protected: virtual char_type do_decimal_point() const; virtual char_type do_thousands_sep() const; virtual string do_grouping() const; virtual string_type do_truename() const; virtual string_type do_falsename() const; }; template <class charT> class numpunct_byname : public numpunct<charT> { public: explicit numpunct_byname(const char*, size_t refs = 0); }; }
char_type
The type of the template argument.
string_type
Class template basic_string specialized on char_type.
explicit numpunct(size_t refs = 0);
Constructs a numpunct object. Calls locale::facet (refs).
The refs argument is set to the initial value of the object's reference count. A numpunct object f constructed with (refs == 0) that is installed in one or more locale objects will be destroyed and the storage it occupies will be deallocated when the last locale object containing the facet is destroyed, as if by calling delete static_cast<locale::facet*>(&f). A numpunct object constructed with (refs != 0) will not be destroyed by any locale objects in which it may have been installed.
explicit numpunct_byname(const char* name, size_t refs = 0);
Constructs a numpunct_byname object for the locale given by name. Calls numpunct<char_type>::numpunct (refs).
static locale::id id;
Unique identifier for this type of facet.
The public members of the numpunct facet include an interface to protected members. Each public member function xxx() calls a corresponding virtual protected member do_xxx(). For instance, the public function grouping() simply calls its protected cousin do_grouping().
char_type decimal_point() const; string_type falsename() const; string grouping() const; char_type thousands_sep() const; string_type truename() const;
Each of these public member functions xxx()returns the result of the corresponding virtual protected do_xxx() function. The result of the call my be cached. Subsequent calls to the function may return the cached result to avoid the expense of repeatedly calling the virtual function.
virtual char_type do_decimal_point() const;
Returns the decimal radix separator. In the C locale, the function returns the period `.' (and L'.').
virtual string_type do_falsename() const; virtual string_type do_truename() const;
Returns a string representing true or false. In the C locale, the functions return "false " and "true", (and L"false" and L"true") respectively.
virtual string do_grouping() const;
Returns a string in which the numeric value of each character is used to represent the number of digits in a particular group, starting with the rightmost group. A group is a sequence of digits between two thousands separators, or between the decimal point and the first thousands separator. Each group at a position greater than or equal to the size of the string gets the same value as the last element in the string. If a value is less than or equal to zero, or equal to CHAR_MAX, then the size of that group is unlimited. In the C locale, the function returns an empty string, indicating no grouping.
virtual char_type do_thousands_sep() const;
Returns the digit group separator used to separate groups of digits in the integer part of numeric values. In the C locale, the function returns the comma `,' (and L',').
#include <iostream> // for cout, endl #include <locale> // for locale, numpunct, use_facet #include <stdexcept> // for runtime_error #if defined (_WIN32) || defined (_WIN64) # define GERMAN_LOCALE "german_germany.1252" #elif defined (__osf__) # define GERMAN_LOCALE "de_DE.88591" #elif defined (SNI) # define GERMAN_LOCALE "De_DE.88591" #elif defined (_AIX) # define GERMAN_LOCALE "de_DE.ISO8859-1" #elif defined (__linux__) # define GERMAN_LOCALE "de_DE" #elif defined (__hpux) # define GERMAN_LOCALE "de_DE.iso88591" #else # define GERMAN_LOCALE "de" #endif int main () { try { // obtain a numpunct facet for the german locale const std::numpunct<char> &np = std::use_facet<std::numpunct<char> >(std::locale (GERMAN_LOCALE)); std::cout << "Decimal point = " << np.decimal_point () << "\nThousands seperator = " << np.thousands_sep () << "\nTrue name = " << np.truename () << "\nFalse name = " << np.falsename() << std::endl; } catch (std::runtime_error& e) { // a runtime_error will be thrown if the locale cannot be constructed std::cout << "Caught runtime_error:\n"; std::cout << e.what() << '\n'; } catch (...) { std::cout << "Caught an unknown exception\n"; } return 0; } Program Output: Decimal point = , Thousands seperator = . True name = true False name = false
locale, Facets, num_put, num_get, ctype
ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 22.2.3.1