首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::num_put

Defined in header <locale>

?

?

template< class CharT, class OutputIt = std::ostreambuf_iterator<CharT> > class num_put;

?

?

std::num_put将用于格式化数字值的规则封装为字符串。具体来说,类型bool,,,long,,,unsigned long,,,long long,,,unsigned long long,,,double,,,long double,,,void*,以及所有类型中隐式可转换到这些%28的类型,例如intfloat%29得到支持。标准格式输出运算符%28,如cout << n;%29使用std::num_putI/O流%27s区域设置的方面,以生成数字的文本表示形式。

二次

二次

继承图

类型要求

-输出必须符合输出器的要求。

*。

专门性

标准库提供了两个独立的%28区域独立的%29完全专门化和两个部分专门化:

在标头中定义<locale>

*。

STD::NUM[医]放<char>创建数字的窄字符串表示

STD::NUM[医]放置<wchar[医]T>创建数字的宽字符串表示

STD::NUM[医]PUT<char,OutputIt>使用自定义输出迭代器创建数字的窄字符串表示

STD::NUM[医]放置<wchar[医]T,OutputIt>使用自定义输出迭代器创建数字的宽字符串表示

此外,在C++程序中构造的每个locale对象都实现了自己的%28 locale特定于这些专门化的%29版本。

成员类型

Member type

Definition

char_type

CharT

iter_type

OutputIt

成员函数

(constructor)

constructs a new num_put facet (public member function)

(destructor)

destructs a num_put facet (protected member function)

put

invokes do_put (public member function)

受保护成员函数

do_put virtual

formats a number and writes to output stream (virtual protected member function)

成员对象

static std::locale::id id

id of the locale (public member object)

二次

代码语言:javascript
复制
#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";
    // use the facet directly
    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';
}

二次

产出:

二次

代码语言:javascript
复制
Direct conversion to string:
1234567.890000
Output using a german locale:
1.234.567,890000
Output using an american locale:
1,234,567.890000

二次

另见

numpunct

defines numeric punctuation rules (class template)

num_get

parses numeric values from an input character sequence (class template)

to_string (C++11)

converts an integral or floating point value to string (function)

to_wstring (C++11)

converts an integral or floating point value to wstring (function)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com