/// 1. 补位,域宽问题
#include<iostream>
using namespace std;
int main()
{
int num;
num=5;
cout.fill('0');//设置填充字符
cout.width(5);//设置域宽
cout<<num<<endl;
return 0;
}
结果:00005
/// 2. 小数点以及精度问题
#include <iostream>
#include <iomanip> ///头文件很重要
using namespace std;
const double value = 12.3456789;
int main()
{
cout.precision(5); ///输出 5 位数字 超出原数字位数会 输出原数字 (会四舍五入)
cout << value << endl;
cout.precision(6);
cout.setf(ios::fixed); ///输出 6 位小数 超出原数字小数位数会 补0 (会四舍五入)
cout << value << endl;
cout<<setiosflags(ios::fixed)<<setprecision(6)<<value<<endl; ///同上
return 0;
}
结果:
12.345
12.345679 (四舍五入)
12.345679 (四舍五入)