std::money_put<CharT,OutputIt>::put, do_put
| ヘッダ <locale> で定義
|
||
public: iter_type put(iter_type out, bool intl, std::ios_base& f, char_type fill, long double quant) const; |
(1) | |
iter_type put(iter_type out, bool intl, std::ios_base& f, char_type fill, const string_type& quant) const; |
(2) | |
protected: virtual iter_type do_put(iter_type out, bool intl, std::ios_base& str, char_type fill, long double units) const; |
(3) | |
virtual iter_type do_put(iter_type out, bool intl, std::ios_base& str, char_type fill, const string_type& digits) const; |
(4) | |
金額の値を書式化し、その結果を出力ストリームに書き込みます。
do_put を呼びます。ct.widen(buf1, buf1 + std::sprintf(buf1, "%.0Lf", units), buf2) によって行われたかのように、数値引数 units がワイド文字列に変換されます。 ただし ct は str.getloc() に設定されている std::ctype ファセットであり、 buf1 および buf2 は十分大きな文字バッファです。 結果の文字列 buf2 が下で説明するように処理され、書式化され、 out に出力されます。digits から、オプショナルな先行マイナス記号 (ct.widen('-') と比較することによって決定されます、ただし ct は str.getloc() に設定されている std::ctype ファセットです) および直後の数字 (ct によって分類されます) のみが、下で説明するように処理され、書式化され、 out に出力されるための文字シーケンスとして取られます。前のステップからの文字シーケンスが与えられたとき、最初の文字が ct.widen('-') と等しい場合は書式化の pattern を取得するために mp.neg_format() を呼び、そうでなければ mp.pos_format() を呼びます。 ただし mp は str.getloc() に設定されている std::moneypunct<CharT, intl> ファセットです。
mp.grouping()、 mp.frac_digits()、 mp.decimal_point() および mp.thousands_sep() によって要求される通りに桁区切り文字および小数点文字が挿入され、結果の文字列が書式化パターン内に value が現れる出力シーケンス内の位置に置かれます。
str.flags() & str.showbase が非ゼロ (std::showbase マニピュレータが使用された) の場合は、 mp.curr_symbol() を呼ぶことによって通貨記号または通貨文字列が生成され、書式化パターン内に symbol が現れる出力シーケンス内の位置に置かれます。
mp.positive_sign() (正の書式パターンが使用される場合) または mp.negative_sign() (負の書式パターンが使用される場合) が2文字以上の文字列を返した場合、返された最初の文字は書式化パターン内に sign が現れる出力シーケンス内の位置に置かれ、残りの文字は他のすべての文字の後に置かれます。 例えば、 123 の units および "-" の negative_sign を伴った書式化パターン {sign, value, space, symbol} は "-1.23 €" を生成するのに対して、 "()" の negative_sign は "(1.23 €)" を生成します。
指定された書式に対して生成された文字数が str.width() によって返された値より小さい場合は、出力シーケンスの合計長を str.width() にするために、以下のように fill のコピーが挿入されます。
str.flags() & str.adjustfieldがstr.internalと等しい場合、フィル文字は書式化パターン内にnoneまたはspaceが現れる位置に挿入されます。- そうでなく、
str.flags() & str.adjustfieldがstr.leftと等しい場合、fillのコピーは他のすべての文字の後に追加されます。 - そうでなければ、フィル文字は他のすべての文字の前に置かれます。
最後に、あらゆる std::setw の効果を取り消すために str.width(0) を呼びます。
戻り値
生成された最後の文字の直後を指すイテレータ。
ノート
通貨単位はその通貨の最も小さな非小数単位であると仮定されます。 米国のセント、日本の円などです。
例
#include <iostream>
#include <iomanip>
#include <locale>
struct my_punct : std::moneypunct_byname<char, false> {
my_punct(const char* name) : moneypunct_byname(name) {}
string_type do_negative_sign() const { return "()"; }
};
int main()
{
std::locale loc("ru_RU.utf8");
std::cout.imbue(loc);
long double units = -123.45;
std::cout << "In Russian locale, " << units << " prints as "
<< std::showbase;
// note, the following is equivalent to simply std::put_money(units)
std::use_facet<std::money_put<char>>(loc).put(
{std::cout}, false, std::cout, std::cout.fill(), units);
std::cout << '\n';
std::cout.imbue(std::locale(std::cout.getloc(), new my_punct("ru_RU.utf8")));
std::cout << "With negative_sign set to \"()\", it prints as ";
std::use_facet<std::money_put<char>>(loc).put(
{std::cout}, false, std::cout, std::cout.fill(), units);
std::cout << '\n';
}
出力:
In Russian locale, -123,45 prints as -1.23 руб
With negative_sign set to "()", it prints as (1.23 руб)
関連項目
| std::money_get および std::money_put で使用される金額の書式パラメータを定義します (クラステンプレート) | |
| 入力文字シーケンスから金額の値をパースおよび構築します (クラステンプレート) | |
(C++11) |
金額をフォーマットして出力します (関数テンプレート) |