c语言四舍五入保留小数
#include<stdio.h>
int main()
{
double sum = 0.0;
scanf("%lf",&sum);
sum = (sum*100+5)/100.0;
printf("%.1lf\n",sum);
return 0;
}
精华在第7行, 举个例子吧,
eq:sum = 3.6589
sum = sum*100+5 = 370.89
sum/100 = 3.7089
printf(“%.1lf\n”,sum) // .1的意思是保留一位小数
这样结果就为3.7了