C++设置小数点后保留位数的代码如下:
double a = 12.123;
cout << "1: " << a << endl;
cout.setf(ios::fixed); //使用固定符号生成浮点类型
cout.precision(2); //设置小数点精度为两位
cout << "2: " << a << endl;
cout.unsetf(ios::fixed); //取消固定
cout.precision(6); //恢复默认精度6
cout << "3: " << a << endl;
cout.precision(2); //未使用cout.setf(ios::fixed)固定小数点
//此时作用是设置总精度为两位
cout << "4: " << a << endl;
运行结果:
1: 12.123
2: 12.12
3: 12.123
4: 12
826

被折叠的 条评论
为什么被折叠?



