欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

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
}