C 语言

关注公众号 jb51net

关闭
首页 > 软件编程 > C 语言 > C++浮点数输出位数控制

C++浮点数输出位数控制方式

作者:墨尽佯書

这篇文章主要介绍了C++浮点数输出位数控制方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

在 C++ 中控制浮点数的输出格式(精度、位数、格式)是一项常用技能。

以下从基础到进阶详细讲解。

一、头文件

控制浮点数输出需要包含以下头文件:

#include <iostream>
#include <iomanip>  // 必须包含,提供格式化操作符

二、常用控制符

控制符作用
fixed使用定点十进制表示法(固定小数位数)
scientific使用科学计数法
defaultfloat恢复默认格式(C++11)
setprecision(n)设置有效数字位数(默认)或小数位数(与 fixed 连用)
setw(n)设置字段宽度
setfill(ch)设置填充字符

三、核心知识点:setprecision的两种模式

1. 默认模式:设置有效数字位数

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
    double num = 123.456789;
    cout << setprecision(3) << num << endl;  // 输出:123
    cout << setprecision(4) << num << endl;  // 输出:123.5
    cout << setprecision(5) << num << endl;  // 输出:123.46
    cout << setprecision(6) << num << endl;  // 输出:123.457
    return 0;
}

注意:默认模式下,setprecision 控制的是总有效数字位数,且会自动四舍五入,末尾多余的 0 会被省略。

2. 配合fixed使用:设置小数点后位数

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
    double num = 123.456789;
    cout << fixed << setprecision(2) << num << endl;  // 输出:123.46
    cout << fixed << setprecision(4) << num << endl;  // 输出:123.4568
    cout << fixed << setprecision(6) << num << endl;  // 输出:123.456789
    return 0;
}

注意

四、科学计数法

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
    double num = 123.456789;
    cout << scientific << setprecision(2) << num << endl;  // 输出:1.23e+02
    cout << scientific << setprecision(4) << num << endl;  // 输出:1.2346e+02
    return 0;
}

五、字段宽度与填充

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
    double num = 12.34;
    // 设置宽度为10,默认右对齐
    cout << setw(10) << num << endl;           // 输出:"     12.34"
    // 设置宽度 + 填充字符
    cout << setfill('*') << setw(10) << num << endl;  // 输出:"******12.34"
    // 左对齐
    cout << left << setw(10) << num << endl;   // 输出:"12.34     "
    return 0;
}

六、组合使用示例

示例 1:控制小数点后 2 位,宽度为 10,右对齐

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
    double num = 3.1415926;
    cout << fixed << setprecision(2) << setw(10) << num << endl;
    // 输出:"      3.14"
    return 0;
}

示例 2:货币格式(保留两位小数)

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
    double price = 19.99;
    double total = 123.4;
    cout << fixed << setprecision(2);
    cout << "Price: $" << price << endl;   // 输出:Price: $19.99
    cout << "Total: $" << total << endl;   // 输出:Total: $123.40
    return 0;
}

七、恢复默认设置

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
    double num = 123.456;
    // 修改格式
    cout << fixed << setprecision(2) << num << endl;  // 123.46
    // 恢复默认格式(C++11 之后)
    cout << defaultfloat << setprecision(6) << num << endl;  // 123.456
    return 0;
}

八、完整对比表格

代码输出 (num = 123.456789)
cout << num;123.457
cout << setprecision(4) << num;123.5
cout << fixed << num;123.456789
cout << fixed << setprecision(2) << num;123.46
cout << scientific << setprecision(2) << num;1.23e+02
cout << setw(10) << setprecision(3) << num;" 123"

九、常见问题与注意事项

1.setprecision是粘性的

一旦设置,后续所有浮点数输出都会受影响,直到重新设置。

2. 整数与浮点数混用

int a = 100;
cout << fixed << setprecision(2) << a << endl;  // 输出:100.00

整数会被隐式转换为浮点数输出。

3. 使用#include <iomanip>才能使用setprecision、setw等

4. 避免使用printf风格

C++ 推荐使用 cout 配合 iomanip,类型安全且可扩展。

十、快速参考卡片

// 保留小数点后 2 位
cout << fixed << setprecision(2) << num;
// 保留 4 位有效数字
cout << setprecision(4) << num;
// 科学计数法,小数点后 3 位
cout << scientific << setprecision(3) << num;
// 宽度 10,左对齐,填充 '*'
cout << left << setfill('*') << setw(10) << num;
// 组合使用
cout << fixed << setprecision(2) << setw(8) << num;
// 恢复默认
cout << defaultfloat;

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文