Library: Localization
money_put locale::facet
A monetary formatting facet
#include <locale> namespace std { template <class charT, class OutputIterator = ostreambuf_iterator<charT> > class money_put; }
The primary template can be implicitly or explicitly specialized on any character type that satisfies the requirements on the type of the character used by iostream class templates, and on any iterator type that satisfies the requirements of Output Iterator.
The money_put facet includes facilities for formatted output of monetary values.
namespace std { template <class charT, class OutputIterator = ostreambuf_iterator<charT> > class money_put : public locale::facet { public: typedef charT char_type; typedef OutputIterator iter_type; typedef basic_string<charT> string_type; explicit money_put(size_t = 0); iter_type put(iter_type, bool, ios_base&, char_type, long double) const; iter_type put(iter_type, bool, ios_base&, char_type, const string_type&) const; static locale::id id; protected: virtual iter_type do_put(iter_type, bool, ios_base&, char_type, long double) const; virtual iter_type do_put(iter_type, bool, ios_base&, char_type, const string_type&) const; }; }
char_type
Type of the first template argument.
iter_type
Type of the second template argument.
string_type
Type of basic_string specialized on char_type.
explicit money_put(size_t refs = 0)
Constructs a money_put object. Calls locale::facet (refs).
The refs argument is set to the initial value of the object's reference count. A money_put 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 money_put object constructed with (refs != 0) will not be destroyed by any locale objects in which it may have been installed.
static locale::id id;
Unique identifier for this type of facet.
The public members of the money_put facet include an interface to protected members. Each public member put() calls the corresponding virtual protected member do_put().
iter_type put(iter_type s, bool intl, ios_base& io, char_type fill, long double units) const; iter_type put(iter_type s, bool intl, ios_base& io, char_type fill, const string_type& digits) const;
Each of these two overloads of the public member function put() calls the corresponding protected virtual function do_put().
virtual iter_type do_put(iter_type s, bool intl, ios_base& io, char_type fill, long double units) const; virtual iter_type do_put(iter_type s, bool intl, ios_base& io, char_type fill, const string_type& digits) const;
The functions format the integral part of the monetary value in units, or the initial sequence of digits optionally preceded by the minus sign, '-', in digits, respectively, according to the format specified by the formatting flags set in io and the moneypunct<char_type, intl> facet installed in the locale imbued in io, using the fill argument for padding as appropriate, and inserts it into the output sequence starting at s. Just before they return an iterator pointing one past the last character written, the functions call io.width(0).
The functions obtain a reference, punct, to the moneypunct<char_type, intl> facet installed in the locale object imbued in io to determine the format of the produced sequence as if by calling use_facet<moneypunct<char_type, intl> >(io.getloc()).
Similarly, the functions obtain a reference, ctype, to the ctype<char_type> facet installed in the locale object imbued in io to determine the wide versions of the characters inserted into the output sequence as if by calling use_facet<ctype<char_type> >(io.getloc()).
The functions use the result of punct.pos_format() to format positive monetary values, and the result of neg_format() to format negative values.
Where money_base::none appears in the format, and if ((io.flags() & ios_base::adjustfield) == ios_base::internal), and if io.width(), w, is a value greater than the number of characters that would have otherwise been inserted without any padding, the functions insert a sufficient number of copies of the fill character to make the total number of inserted characters equal to w.
Where money_base::sign appears in the format, the first character of punct.negative_sign(), if the monetary value units is negative or if ('-' == ctype.narrow (digits[0], '\0')) is true, or positive_sign(). Otherwise, it is inserted into the output sequence. No character is inserted if the respective sign string is empty.
Where money_base::space appears in the format, a single space character returned from ctp.widen (' ') is inserted into the output sequence, followed by zero or more copies of the fill character according to the same rules as for money_base::none.
Where money_base::symbol appears in the format, if (io.flags() & ios_base::showbase) is non-zero, the string returned from punct.curr_symbol() is inserted into the output sequence.
Where money_base::value appears in the format, a sequence of digits representing the absolute value, v, of the integral portion of the monetary value is inserted into the output sequence. If (fd > 0) is true for the value fd equal to punct.frac_digits(), the character returned from punct.decimal_point() is inserted into the sequence so that it is followed by exactly fd digits of v, possibly preceded by the appropriate number of zeros as returned from ctype.widen('0'). If punct.grouping() returns a non-empty string, groups of inserted digits will be separated by the character returned from punct.thousands_sep() as appropriate.
If io.width(), w, is a value greater than the number of characters that would have otherwise been inserted without any padding, then for the result, adj, of the expression(io.flags() & ios_base::adjustfield), if (adj == ios_base::left) is false, the functions insert a sufficient number of copies of the fill character before inserting any other characters into the ouptut sequence to make the total number of inserted characters equal to w; otherwise, if (adj != ios_base::internal) is true, the functions returns the same number of copies of fill after all other characters have been inserted.
#include <locale> // for locale, money_put, use_facet #include <iostream> // for cout, endl #include <iterator> // for ostreambuf_iterator #include <string> // for string int main () { typedef std::ostreambuf_iterator<char, std::char_traits<char> > Iter; const std::string buffer ("10002"); const long double ldval = 10002; // Construct a ostreambuf_iterator on cout Iter begin (std::cout); const std::locale loc; // Get a money put facet const std::money_put<char, Iter> &mp = std::use_facet<std::money_put<char, Iter> >(loc); // Put out the string representation of the monetary value std::cout << buffer << " --> "; mp.put (begin, false, std::cout, ' ', buffer); // Put out the long double representation of the monetary value std::cout << std::endl << ldval << " --> "; mp.put (begin, false, std::cout, ' ', ldval); std::cout << std::endl; return 0; } Program Output: 10002 --> 10002 10002 --> 10002
locale, Facets, money_get, moneypunct
ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 22.2.6.2