std::money_put<CharT,OutputIt>::put, do_put
| ヘッダー <locale> で定義 |
||
| public: iter_type put( iter_type out, bool intl, std::ios_base& f, |
(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, |
(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('-') と等しい場合、フォーマット 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()(負のフォーマットパターンの場合)が1文字より長い文字列を返す場合、最初に返された文字はフォーマットパターンで sign が現れる位置に出力シーケンスに配置され、残りの文字は他のすべての文字の後ろに配置されます。例えば、単位が 123 で 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 のコピーは他のすべての文字の後ろに追記されます。
- それ以外の場合、フィル文字は他のすべての文字の前に配置されます。
最後に、str.width(0) を呼び出して、std::setw の効果をキャンセルします。
目次 |
[編集] 返り値
生成された最後の文字の直後を指すイテレータ。
[編集] 注記
通貨単位は、通貨の最小の非小数単位であると想定されます。米国ではセント、日本では円。
[編集] 例
#include <iomanip> #include <iostream> #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 руб)
[編集] 不具合報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 328 | C++98 | "%.01f" だった std::sprintf のためのフォーマット文字列 | "%.0Lf" に修正 |
[編集] 関連項目
| std::money_get と std::money_put で使用される通貨書式設定パラメータを定義する (クラステンプレート) | |
| 入力文字列シーケンスから通貨値を解析・構築する (クラステンプレート) | |
| (C++11) |
通貨の値をフォーマットして出力する (関数テンプレート) |