修改C++ 中cout默认输出6位精度
默认情况下,cout默认输出6位有效数字(这6位包括小数点前的数字)
double value = 12.3456789;
// 默认精度是 6,所以输出为 12.3457
//(默认情况下,精度是指总的有效数字)
cout << value << endl;
// 把精度修改为 4, 输出 12.35, 对最后一位四舍五入
// 精度修改后,持续有效,直到精度再次被修改
cout.precision(4);
cout << value << endl;
// 使用定点法, 精度变成小数点后面的位数
// 输出 12.3457
cout.flags(cout.fixed);
cout << value << endl;
// 定点法持续有效
// 输出 3.1416
cout << 3.1415926535 << endl;
// 把精度恢复成有效数字位数
cout.unsetf(cout.fixed);
cout << value << endl; //输出 12.35
cout << 3.1415926535 << endl;