C语言实现四舍五入算法

版权声明:感谢观看我的博客https://me.csdn.net/weixin_43794314 https://blog.csdn.net/weixin_43794314/article/details/85223682

四舍五入:
如果精确到小数点后面的第n位,则需要对n+1位进行运算。
将小数乘以10的n+1次方后加5,然后除以10强制转换为长整型;再将该数除以10的n次方并转换为浮点型。

float fun(float m)
{
	int temp;
	temp = (int) (m*1000 + 5) / 10;
	float n = (float) temp / 100.0;
	return n;
}

猜你喜欢

转载自blog.csdn.net/weixin_43794314/article/details/85223682