1 #include <iostream> 2 #include <Windows.h> 3 4 using namespace std; 5 6 int main(void){ 7 double value = 12.3456789; 8 9 //默认情况下,cout输出6位有效数字 10 cout << value << endl; 11 12 //可以修改输出的精度 13 cout.precision(4); //修改cout的输出精度为4 14 cout << value << endl;//12.35 15 16 //如果想要这个精度,用来表示小数点后面的位数 17 cout.flags(cout.fixed); //定点法:精度,用来表示小数点后面的位数 18 cout << value << endl; 19 20 cout << 3.1415926 << endl; 21 22 cout.unsetf(cout.fixed);//取消定点法 23 cout << 3.1415926 << endl; 24 25 system("pause"); 26 return 0; 27 }
输出结果: