C++ 浮点数输出位数控制详解
在 C 中控制浮点数的输出格式精度、位数、格式是一项常用技能。以下从基础到进阶详细讲解。一、头文件控制浮点数输出需要包含以下头文件cpp#include iostream #include iomanip // 必须包含提供格式化操作符二、常用控制符控制符作用fixed使用定点十进制表示法固定小数位数scientific使用科学计数法defaultfloat恢复默认格式C11setprecision(n)设置有效数字位数默认或小数位数与 fixed 连用setw(n)设置字段宽度setfill(ch)设置填充字符三、核心知识点setprecision的两种模式1. 默认模式设置有效数字位数cpp#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使用设置小数点后位数cpp#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 会保留该模式一旦设置后续所有浮点数输出都会受影响直到修改四、科学计数法cpp#include iostream #include iomanip using namespace std; int main() { double num 123.456789; cout scientific setprecision(2) num endl; // 输出1.23e02 cout scientific setprecision(4) num endl; // 输出1.2346e02 return 0; }五、字段宽度与填充cpp#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右对齐cpp#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货币格式保留两位小数cpp#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; }七、恢复默认设置cpp#include iostream #include iomanip using namespace std; int main() { double num 123.456; // 修改格式 cout fixed setprecision(2) num endl; // 123.46 // 恢复默认格式C11 之后 cout defaultfloat setprecision(6) num endl; // 123.456 return 0; }八、完整对比表格代码输出 (num 123.456789)cout num;123.457cout setprecision(4) num;123.5cout fixed num;123.456789cout fixed setprecision(2) num;123.46cout scientific setprecision(2) num;1.23e02cout setw(10) setprecision(3) num; 123九、常见问题与注意事项1.setprecision是粘性的一旦设置后续所有浮点数输出都会受影响直到重新设置。2. 整数与浮点数混用cppint a 100; cout fixed setprecision(2) a endl; // 输出100.00整数会被隐式转换为浮点数输出。3. 使用#include iomanip才能使用setprecision、setw等4. 避免使用printf风格C 推荐使用cout配合iomanip类型安全且可扩展。十、快速参考卡片cpp// 保留小数点后 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;
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2444704.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!