C++保留2位小数输出

转载自http://www.cnblogs.com/stacktrace/p/5142470.html


1. 保留两位小数输出

#include <iomanip>
double res = 3.1415926;
cout << fixed << setprecision(2) << res << endl;
输出结果为3.14
2. 保留2个有效数字输出

#include <iomanip>
double res = 3.1415926;
cout << setprecision(2) << res << endl;

输出结果为3.1,注意结果会四舍五入。

猜你喜欢

转载自blog.csdn.net/SharonLu1216/article/details/78082701