c++ 保留重要位数和小数位数 -II.
最编程
2024-04-23 09:01:58
...
采用四舍五入方式。
int main()
{
double a = 3.141564;
cout << setiosflags(ios::fixed) << setprecision(3) << a; // a = 3.142
return 0;
}
采用舍去法保留有效数字。
int main()
{
double a = 3.14555;
a = floor(a * 1000) / 1000.0;
cout << setiosflags(ios::fixed) << setprecision(3) << a; // a = 3.145
}
上一篇: python 保留有效数字
下一篇: 保留小数的问题||有效数字 (C)