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;
}注意:
fixed表示使用定点表示法,此时setprecision控制的是小数点后的位数- 会补足末尾的 0,例如
setprecision(4)输出123.4568,末尾的 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;总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
C++中CString string char* char 之间的字符转换(多种方法)
在写程序的时候,我们经常遇到各种各样的类型转换,比如 char* CString string 之间的互相转换,这里简单为大家介绍一下,需要的朋友可以参考下2017-09-09
C++利用std::forward_list查找插入数据方法示例
这篇文章主要给大家介绍了关于C++利用std::forward_list查找插入数据的相关资料,文中先对std::forward_list进行了详细的介绍,而后通过示例代码给大家介绍了查找的方法,需要的朋友可以参考借鉴,下面话不多说了,来一起看看吧。2017-08-08


最新评论