cout 默认输出格式相当于 c 里 %g 格式 -- 简略格式,例如 123.00000 输出为 123
123.450000 输出为123.45
用 cout << fixed << 默认输出格式相当于 c 里 %f 和 %lf 格式
如果想指定 输出小数位数,则要 加 头文件 #include <iomanip>, 调用 setprecision(多少位)。
=======
例子:
#include <iostream>
#include <iomanip>
using namespace std;
main(){
double x=123.0;
cout << x << endl;
cout << fixed << x << endl;
cout<<setiosflags(ios::fixed); //保证setprecision()是设置小数点后的位数。
cout << setprecision(2) << x << endl;
}
例子输出:
123
123.000000
123.00