题目:
请编一个函数float fun(double h),函数的功能是对变量h 中的值保留2 位小数,并对第三位进行四舍五入(规定h 中的值为正数)。
例如:h 值为8.32433,则函数返回8.32;
h 值为8.32533,则函数返回8.33。
代码展示:
#include <stdio.h>
float fun(double h)
{
float result;
result=(h * 100 + 0.05) / 100.0;
return result;
}
int main()
{
float h;
printf("please enter a number:\n");
scanf("%f", &h);
printf("result=%.2f",fun(h));
return 0;
}