C++ 标准库 - <iostream>

C++ std::fixed 把浮点输出切换为定点小数形式:始终使用小数点,setprecision 表示小数点后的位数。该格式会保持,直到改用 defaultfloat 或 scientific。

原型

ios_base& fixed(ios_base& str);

参数

  • 作为操纵符插入输出流,无额外参数。

返回值

返回流引用。

示例

#include <iomanip>
#include <iostream>

int main() {
    double v = 3.1415926;
    std::cout << std::fixed << std::setprecision(2) << v << std::endl;
    return 0;
}

输出:

3.14

注意事项

金额、表格等需要对齐小数位时,常用 fixed 加 setprecision。科学计数用 scientific。恢复默认用 defaultfloat。

下一页介绍hex:十六进制整数输出。