cout格式化输出

来源:互联网 发布:广电总局网络主播新规 编辑:程序博客网 时间:2024/06/07 02:01

1.小数位数

可以使用setprecision()来确定小数位数,加fixed补充末位的0

需要添加头文件

#include<iomanip>

假设 ans=1.202;
以下是保留两位小数,若末尾是0,则省略

cout << setprecision(2) << ans <<endl;//输出1.2

若想不省略末尾的0,则需要加上fixed

cout.setf(ios::fixed);cout << fixed << setprecision(2) << ans << endl;//输出1.20

若想恢复省略

cout.unsetf(ios::fixed);  cout << setprecision(2) << ans <<endl;//输出1.2
1 0
原创粉丝点击