命名空间
变体
操作

std::fixed、 std::scientific、 std::hexfloat、 std::defaultfloat

来自 cppreference.cn
< cpp‎ | io‎ | manip
 
 
 
输入/输出 操纵符
浮点格式化
fixedscientifichexfloatdefaultfloat
(C++11)(C++11)
整数格式化
布尔格式化
字段宽度和填充控制
其他格式化
空白处理
输出刷新
(C++20)  

状态标志操作
时间和货币 I/O
(C++11)
(C++11)
(C++11)
(C++11)
引用操纵符
(C++14)
 
定义于头文件 <ios>
(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) 将流 strfloatfield 设置为 fixed,如同调用 str.setf(std::ios_base::fixed, std::ios_base::floatfield) 一样。
2) 将流 strfloatfield 设置为 scientific,如同调用 str.setf(std::ios_base::scientific, std::ios_base::floatfield) 一样。
3) 同时将流 strfloatfield 设置为 fixedscientific,如同调用 str.setf(std::ios_base::fixed | std::ios_base::scientific, std::ios_base::floatfield) 一样。 这会启用十六进制浮点格式化。
4) 将流 strfloatfield 设置为零,如同调用 str.unsetf(std::ios_base::floatfield) 一样。 这会启用默认浮点格式化,它与 fixed 和 scientific 不同。

这是一个 I/O 操纵符,可以像 out << std::fixed 这样 для 任何 out 类型为 std::basic_ostream 的表达式调用(或者像 in >> std::scientific 这样 для 任何 in 类型为 std::basic_istream 的表达式)。

目录

[编辑] 参数

str - I/O 流的引用

[编辑] 返回值

str (操纵后的流的引用)。

[编辑] 注释

十六进制浮点格式化忽略流精度规范,这是 std::num_put::do_put 规范所要求的。

这些操纵符不影响浮点解析。

[编辑] 示例

#include <iomanip>
#include <iostream>
#include <sstream>
 
enum class cap { title, middle, end };
 
void print(const char* text, double num, cap c)
{
    if (c == cap::title)
        std::cout <<
            "┌──────────┬────────────┬──────────────────────────┐\n"
            "│  number  │   iomanip  │      representation      │\n"
            "├──────────┼────────────┼──────────────────────────┤\n";
    std::cout << std::left
         << "│ " << std::setw(8) << text <<      " │ fixed      │ "
         << std::setw(24) << std::fixed  << num <<            " │\n"
         << "│ " << std::setw(8) << text <<      " │ scientific │ "
         << std::setw(24) << std::scientific << num <<        " │\n"
         << "│ " << std::setw(8) << text <<      " │ hexfloat   │ "
         << std::setw(24) << std::hexfloat << num <<          " │\n"
         << "│ " << std::setw(8) << text <<      " │ default    │ "
         << std::setw(24) << std::defaultfloat << num <<      " │\n";
    std::cout << (c != cap::end ?
            "├──────────┼────────────┼──────────────────────────┤\n" :
            "└──────────┴────────────┴──────────────────────────┘\n");
}
 
int main()
{
    print("0.0", 0.0, cap::title);
    print("0.01", 0.01, cap::middle);
    print("0.00001", 0.00001, cap::end);
 
    // Note; choose clang for correct output
    double f;
    std::istringstream("0x1.8p+0") >> f;
    std::cout << "Parsing 0x1.8p+0 gives " << f << '\n';
 
    std::istringstream("0x1P-1022") >> f;
    std::cout << "Parsing 0x1P-1022 gives " << f << '\n';
}

输出

┌──────────┬────────────┬──────────────────────────┐
│  number  │   iomanip  │      representation      │
├──────────┼────────────┼──────────────────────────┤
│ 0.0      │ fixed      │ 0.000000                 │
│ 0.0      │ scientific │ 0.000000e+00             │
│ 0.0      │ hexfloat   │ 0x0p+0                   │
│ 0.0      │ default    │ 0                        │
├──────────┼────────────┼──────────────────────────┤
│ 0.01     │ fixed      │ 0.010000                 │
│ 0.01     │ scientific │ 1.000000e-02             │
│ 0.01     │ hexfloat   │ 0x1.47ae147ae147bp-7     │
│ 0.01     │ default    │ 0.01                     │
├──────────┼────────────┼──────────────────────────┤
│ 0.00001  │ fixed      │ 0.000010                 │
│ 0.00001  │ scientific │ 1.000000e-05             │
│ 0.00001  │ hexfloat   │ 0x1.4f8b588e368f1p-17    │
│ 0.00001  │ default    │ 1e-05                    │
└──────────┴────────────┴──────────────────────────┘
Parsing 0x1.8p+0 gives 1.5
Parsing 0x1P-1022 gives 2.22507e-308

[编辑] 参见

更改浮点精度
(函数) [编辑]