How do you round in the C language?

In C language, you can use the round() function to achieve rounding. The prototype of the round() function is in the header file, with a return value type of double.

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

int main() {
    double num = 3.6;
    double rounded = round(num);
    
    printf("原始值: %lf\n", num);
    printf("四舍五入后的值: %lf\n", rounded);

    return 0;
}

The output result is:

原始值: 3.600000
四舍五入后的值: 4.000000

Note: The round() function returns a floating point number, to convert the result to an integer, you can use type casting with the int data type.

bannerAds