
#includeiostream #includeiomanip using namespace std; int main(){ //单独使用setprecision(n)输出n位有效数字 double num1 123456789.0; double num2 0.000123456789; double num3 123.456789; cout setprecision(3) num1 endl; //输出1.23e08 cout setprecision(3) num2 endl; //输出0.000123 cout setprecision(3) num3 endl; //输出123 //fixed和setrecision(n)搭配使用指定输出小数点后n位 double num4 123456789.123456789; cout fixed setprecision(3) num4 endl; //输出123456789.123 return 0; }