演示代码如下:
#include <iostream>#include <iomanip>using namespace std;int main(){double a = 123.345;cout << "正常输出为:" << a << endl;cout << "保留两位有效数字:" << setprecision(2) << a << endl;cout << "保留小数点后两位:" << fixed << setprecision(2) << a << endl;return 0;}
结果为:
正常输出为:123.345保留两位有效数字:1.2e+02保留小数点后两位:123.34