std::num_put
来自cppreference.com
                    
                                        
                    
                    
                                                            
                    |   定义于头文件  <locale>
  | 
||
|   template<      class CharT,  | 
||
类 std::num_put 封装格式化数值为字符串的规则。具体而言支持 bool 、 long 、 unsigned long 、 long long 、 unsigned long long 、 double 、 long double 、 void* 类型和所有能隐式转换到它们的类型(例如 int 或 float )。标准格式化输出运算符(如 cout << n; )用 I/O 流的 locale 的 std::num_put 平面生成数字的文本表示。
继承图
类型要求
 -OutputIt 必须满足遗留输出迭代器 (LegacyOutputIterator)  的要求。
 | 
特化
标准库提供二个孤立(独立于本地环境)的全特化和二个部分特化:
|   定义于头文件  
<locale>  | |
| std::num_put<char> | 创建数的窄字符串表示 | 
| std::num_put<wchar_t> | 创建数的宽字符串表示 | 
| std::num_put<char, OutputIt> | 用定制输出迭代器创建数的窄字符串表示 | 
| std::num_put<wchar_t, OutputIt> | 用定制输出迭代器创建数的宽字符串表示 | 
另外, C++ 程序中构造的每个 locale 对象都实装这些特化的其自身(本地环境限定)版本。
成员类型
| 成员类型 | 定义 | 
  char_type
 | 
  CharT
 | 
  iter_type
 | 
  OutputIt
 | 
成员函数
|   构造新的 num_put 平面  (公开成员函数)  | |
|   销毁 num_put 平面  (受保护成员函数)  | |
  调用 do_put  (公开成员函数)  | 
受保护成员函数
|    [虚]  | 
  格式化数字并写入到输出流  (虚受保护成员函数)  | 
成员对象
|    static std::locale::id id  | 
  locale 的 id  (公开成员对象)  | 
示例
运行此代码
#include <iostream> #include <locale> #include <string> #include <iterator> int main() { double n = 1234567.89; std::cout.imbue(std::locale("de_DE")); std::cout << "Direct conversion to string:\n" << std::to_string(n) << '\n' << "Output using a german locale:\n" << std::fixed << n << '\n' << "Output using an american locale:\n"; // 直接使用平面 std::cout.imbue(std::locale("en_US.UTF-8")); auto& f = std::use_facet<std::num_put<char>>(std::cout.getloc()); f.put(std::ostreambuf_iterator<char>(std::cout), std::cout, ' ', n); std::cout << '\n'; }
输出:
Direct conversion to string: 1234567.890000 Output using a german locale: 1.234.567,890000 Output using an american locale: 1,234,567.890000
参阅
|   定义数值标点规则  (类模板)  | |
|   从输入字符序列中解析数字值  (类模板)  | |
|    (C++11)  | 
  转换整数或浮点值为 string (函数)  | 
|    (C++11)  | 
  转换整数或浮点值为 wstring (函数)  |