-
控制cout的输出精度
1.强制以小数的方式显示
cout << fixed;
2.控制显示的精度
cout << setprecision(2);
可以直接写成
cout << fixed << setprecision();
-
控制cout对齐方式及输出位数
#include <iostream>
#include <iomanip>//setw()等函数需要调用这个头文件
using namespace std;
int main()
{
double x1 = 234;
double x2 = 345;
double x3 = 456;
cout << left;//设置对齐方式,默认右对齐
cout << setfill('_');//设置空位置填充符号,默认用空格填充
cout << setw(8) << x1 <<
setw(8) << x2 <<
setw(8) << x3 << endl;
cin.get();//保持输出窗口
return 0;
}
输出效果如图所示