命名空间
变体
操作

std::numpunct

来自 cppreference.cn
< cpp‎ | locale
 
 
 
 
 
定义于头文件 <locale>
template< class CharT >
class numpunct;

facet std::numpunct 封装了数值标点偏好。流 I/O 操作通过 std::num_getstd::num_put 使用 std::numpunct 来解析数值输入和格式化数值输出。

std::numpunct 支持的数字格式如下所述。此处 digit 表示由 fmtflags 参数值指定的基数集,thousands-sepdecimal-point 分别是 thousands_sep()decimal_point() 函数的结果。

整数值的格式如下

integer     ::= [sign] units
sign        ::= plusminus
plusminus   ::= '+' | '-'
units       ::= digits [thousands-sep units]
digits      ::= digit [digits]

thousand-sep 之间数字的位数(digits 的最大大小)由 grouping() 的结果指定。

浮点值的格式如下

floatval    ::= [sign] units [decimal-point [digits]] [e [sign] digits] |
                [sign]        decimal-point  digits   [e [sign] digits]
e           ::= 'e' | 'E'
cpp/locale/locale/facetstd-numpunct-inheritance.svg

继承关系图

内容

[编辑] 特化

标准库保证提供以下特化(它们是任何区域设置对象都必须实现的

定义于头文件 <locale>
std::numpunct<char> 提供 “C” 区域设置偏好的等效项
std::numpunct<wchar_t> 提供 “C” 区域设置偏好的宽字符等效项

[编辑] 嵌套类型

类型 定义
char_type CharT
string_type std::basic_string<CharT>

[编辑] 数据成员

成员 描述
std::locale::id id [静态] facet 的标识符

[编辑] 成员函数

构造一个新的 numpunct facet
(公有成员函数) [编辑]
析构一个 numpunct facet
(受保护的成员函数) [编辑]
调用 do_decimal_point
(公有成员函数) [编辑]
调用 do_thousands_sep
(公有成员函数) [编辑]
调用 do_grouping
(公有成员函数) [编辑]
调用 do_truenamedo_falsename
(公有成员函数) [编辑]

[编辑] 受保护的成员函数

提供用作小数点的字符
(虚函数 受保护的成员函数) [编辑]
提供用作千位分隔符的字符
(虚函数 受保护的成员函数) [编辑]
[虚函数]
提供每对千位分隔符之间的位数
(虚函数 受保护的成员函数) [编辑]
提供用作布尔值 truefalse 名称的字符串
(虚函数 受保护的成员函数) [编辑]

[编辑] 示例

以下示例更改 truefalse 的字符串表示形式

#include <iostream>
#include <locale>
 
struct french_bool : std::numpunct<char>
{
    string_type do_truename() const override { return "vrai"; }
    string_type do_falsename() const override { return "faux"; }
};
 
int main()
{
    std::cout << "default locale: "
              << std::boolalpha << true << ", " << false << '\n';
    std::cout.imbue(std::locale(std::cout.getloc(), new french_bool));
    std::cout << "locale with modified numpunct: "
              << std::boolalpha << true << ", " << false << '\n';
}

输出

default locale: true, false
locale with modified numpunct: vrai, faux

[编辑] 缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。

DR 应用于 已发布行为 正确行为
LWG 338 C++98 sign 标记允许在 +- 后跟随可选的空格 移除了空格

[编辑] 参见

为具名区域设置创建 numpunct facet
(类模板)