C 四舍五入函数

C 语言的 round 函数是数学函数之一,用于返回给定数字或指定表达式的 nearest value(四舍五入后的值)。数学 round 函数的语法是

double round(double round);

round 函数示例

数学 round 函数允许您找到给定数字最接近的整数值。在此程序中,我们将找到不同数字的最接近整数值并显示输出。

#include <stdio.h>
#include <math.h>

int main()
{
    printf("\n The Round Value of 0.75    = %.2f ", round(0.75));
    printf("\n The Round Value of 15.25   = %.2f ", round(15.25));
    printf("\n The Round Value of 152.50  = %.2f ", round(152.50));
    
    printf("\n The Round Value of -14.36  = %.2f ", round(-14.36));
    printf("\n The Round Value of -26.82  = %.2f ", round(-26.32));
    printf("\n The Round Value of -90.50  = %.2f \n", round(-90.50));
    
    return 0;
}
C Round Function Example

math round 示例 2

在此 C 编程示例中,我们允许用户输入自己的值。接下来,该 程序使用 math round 函数来查找四舍五入(最接近)的值。

#include <stdio.h>
#include <math.h> 

int main()
{
    float number, roundValue;
    
    printf(" Please Enter any Numeric Value to Round :  ");
    scanf("%f", &number);
    
    roundValue = round(number);
    
    printf("\n The Round Value of %.2f = %.4f \n", number, roundValue);
    
    return 0;
}
 Please Enter any Numeric Value to Round :  12.23

 The Round Value of 12.23 = 12.0000