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},单位为 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 的效果。
内容 |
[编辑] 返回值
一个迭代器,指向最后一个生成的字符之后的位置。
[编辑] 注释
货币单位被假定为货币的最小非分数单位:美国的 cents,日本的 yen。
[编辑] 示例
#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 | 用于 std::sprintf 的格式字符串是 "%.01f" | 已更正为 "%.0Lf" |
[编辑] 参见
定义了 std::money_get 和 std::money_put 使用的货币格式化参数 (类模板) | |
从输入字符序列解析和构造货币值 (类模板) | |
(C++11) |
格式化和输出货币值 (函数模板) |