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 的效果。
目录 |
返回值
指向最后生成字符之后位置的迭代器。
注释
货币单位假定为该货币的最小非分数单位:在美国为美分,在日本为日元。
示例
#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; // 注意:以下代码等效于直接调用 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)
|
格式化并输出货币值
(函数模板) |