Namespaces
Variants

std:: numpunct

From cppreference.net
定义于头文件 <locale>
template < class CharT >
class numpunct ;

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

std::numpunct 所支持的数字格式如下所述。其中 digit 表示由 fmtflags 参数值指定的基数集合, thousands-sep decimal-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/facet std-numpunct-inheritance.svg

继承关系图

目录

特化

标准库保证提供以下特化(它们 是任何locale对象都必须实现的 ):

定义于头文件 <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 刻面
(公开成员函数)
析构 numpunct 刻面
(受保护成员函数)
调用 do_decimal_point
(公开成员函数)
调用 do_thousands_sep
(公开成员函数)
调用 do_grouping
(公开成员函数)
调用 do_truename do_falsename
(公开成员函数)

受保护成员函数

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

示例

以下示例修改了 true false 的字符串表示:

#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++标准。

缺陷报告 应用于 发布时行为 正确行为
LWG 338 C++98 sign 标记允许在 + - 后存在可选空白符 移除空白符

参见

为指定区域设置创建数字标点facet
(类模板)