请编写一个函数float fun(double h),函数的功能使对变量h中的值保留2位小数,并对第三位进行四舍五入(规定h中的值位正数)。 123.1235678*100--》 12312.35678+0.5

请编写一个函数float fun(double h),函数的功能使对变量h中的值保留2位小数,并对第三位进行四舍五入(规定h中的值位正数)。
123.1235678*100--》 12312.35678+0.5

#include <stdio.h>

float fun(double h) 
{
    float rounded_value = (float)((int)(h * 100 + 0.5)) / 100;
    
    return rounded_value;
}

int main() 
{
    double h = 123.1235678;
    float result = fun(h);

    printf("保留两位小数并四舍五入后的值为: %.2f\n", result);

    return 0;
}

posted on 2024-06-16 23:15  wessf  阅读(41)  评论(0)    收藏  举报