std::hexfloat
Defined in header <ios> | | |
---|---|---|
std::ios_base& fixed( std::ios_base& str ); | (1) | |
std::ios_base& scientific( std::ios_base& str ); | (2) | |
std::ios_base& hexfloat( std::ios_base& str ); | (3) | (since C++11) |
std::ios_base& defaultfloat( std::ios_base& str ); | (4) | (since C++11) |
修改浮点输入/输出的默认格式。
1%29设置floatfield
溪流str
到fixed
好像通过打电话str.setf(std::ios_base::fixed,std::ios_base::floatfield)
2%29设置floatfield
溪流str
到scientific
好像通过打电话str.setf(std::ios_base::scientific,std::ios_base::floatfield)
3%29设置floatfield
溪流str
到fixed
和scientific
同时,好像通过呼叫str.setf(std::ios_base::fixed|std::ios_base::scientific,std::ios_base::floatfield)
这将启用十六进制浮点格式设置。
4%29设置floatfield
溪流str
为零,仿佛通过调用str.unsetf(std::ios_base::floatfield)
这将启用默认浮点格式,这与固定的和科学的格式不同.
这是一个I/O操作程序,可以用表达式调用它,如out << std::fixed
对任何out
类型std::basic_ostream
或使用表达式,如in >> std::scientific
对任何in
类型std::basic_istream
...
参数
str | - | reference to I/O stream |
---|
返回值
str
%28操作后对流的引用%29。
注记
十六进制浮点格式会忽略流精度规范,这是std::num_put::do_put
...
例
二次
#include <iostream>
#include <sstream>
int main()
{
std::cout << "The number 0.01 in fixed: " << std::fixed << 0.01 << '\n'
<< "The number 0.01 in scientific: " << std::scientific << 0.01 << '\n'
<< "The number 0.01 in hexfloat: " << std::hexfloat << 0.01 << '\n'
<< "The number 0.01 in default: " << std::defaultfloat << 0.01 << '\n';
double f;
std::istringstream("0x1P-1022") >> std::hexfloat >> f;
std::cout << "Parsing 0x1P-1022 as hex gives " << f << '\n';
}
二次
产出:
二次
The number 0.01 in fixed: 0.010000
The number 0.01 in scientific: 1.000000e-02
The number 0.01 in hexfloat: 0x1.47ae147ae147bp-7
The number 0.01 in default: 0.01
Parsing 0x1P-1022 as hex gives 2.22507e-308
二次
另见
setprecision | changes floating-point precision (function) |
---|
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com