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('-'),則呼叫 mp.neg_format() 以獲取格式化 模式,否則呼叫 mp.pos_format(),其中 mp 是浸入 str.getloc() 中的 std::moneypunct<CharT, intl> facet。
根據 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()(在使用負數格式模式時)返回的字串包含多個字元,則返回的第一個字元放置在格式化模式中 sign 出現的位置,其餘字元放置在所有其他字元之後,例如,格式化模式 {sign, value, space, symbol},units 為 123,negative_sign 為 "-",可能導致 "-1.23 €",而 negative_sign 為 "()" 則會生成 "(1.23 €)"。
如果為指定格式生成的字元數少於 str.width() 返回的值,則插入 fill 的副本以使輸出序列的總長度恰好為 str.width(),如下所示:
- 如果 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++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 328 | C++98 | 用於 std::sprintf 的格式字串是 "%.01f" | 更正為 "%.0Lf" |
[編輯] 另見
定義 std::money_get 和 std::money_put 使用的貨幣格式引數 (類模板) | |
從輸入字元序列解析和構造貨幣值 (類模板) | |
(C++11) |
格式化並輸出貨幣值 (函式模板) |