C round function

The C round function is one of the math functions used to return the nearest integer value (rounded value) of a given number or a specified expression. For instance, round(5.2) returns 5, and 5.8 returns 6. 

C round() function syntax

The syntax of the math round function is

double round(double round);

Parameter

The round() function accepts a floating-point number (double) as a single parameter to find the nearest integer. By default, it accepts the double data type. However, we can use float, long, and double.

Return Value

The round() function returns the nearest integer (based on the fractional part) as a floating point number.

  • If the fractional part is below 0.5 (0.49 to 0.01), the function will round towards zero.
  • If the fractional part is exactly 0.5 or above (0.51 to 0.99), the function will round away from 0 (towards positive infinity).

To use the round() function, include the <math.h> file.

#include<math.h>

C round function Example

The math round Function allows you to find the closest integer value of a given number. In this program, we will find the closest integer value of different numbers and display the output.

#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 on user-entered float value

In this C Programming example, we are allowing the user to enter their own value. Next, this program uses the math round function to find the rounded (nearest) value.

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

int main()
{
float number, r;

printf(" Please Enter any Numeric Value: ");
scanf("%f", &number);

r = round(number);
printf("\nThe Round Value of %.2f = %.4f \n", number, r);
return 0;
}
 Please Enter any Numeric Value:  12.23
 The Round Value of 12.23 = 12.0000

round() vs roundf() vs roundl()

Apart from the above mentioned round() function, there are two other variants for float and long double data types.

  • roundf(): Accepts a float and returns a float data type.
  • roundl(): Use this function for long double data types.
#include <stdio.h>
#include <math.h>
int main()
{
float a = 25.1f;
long double b = 33.8L;
double c = 10.20;

printf("%.0f\n", roundf(a));
printf("%.0Lf\n", roundl(b));
printf("%.0f\n", round(c));
return 0;
}
25
34
10

C round() vs ceil() vs floor() vs trunc()

The following tables show the behaviour of the four functions for the positive and negative floating-point numbers.

FunctionDescriptionPositive (20.7)Negative (-10.7)
round()Nearest Integer based on the fractional part.21-11
ceil()Smallest integer greater than or equal to the number.21-10
floor()Largest integer less than or equal to the number.20-11
trunc()It removes the fractional part.20-10
#include <stdio.h>
#include <math.h>
int main()
{
double m = 20.7;
double n = -10.8;

printf("round: %.0f\n", round(m));
printf("ceil: %.0f\n", ceil(m));
printf("floor: %.0f\n", floor(m));
printf("trunc: %.0f\n", trunc(m));
printf("--------\n");

printf("round: %.0f\n", round(n));
printf("floor: %.0f\n", floor(n));
printf("ceil: %.0f\n", ceil(n));
printf("trunc: %.0f\n", trunc(n));
return 0;
}
round: 21
ceil: 21
floor: 20
trunc: 20
--------
round: -11
floor: -11
ceil: -10
trunc: -10

round() vs Type Casting

They both return different values for both positive and negative floating-point numbers. Type casting simply truncates the fractional part, whereas the round() function returns the nearest integer.

If you observe the result, when the fractional part is below 0.5, both round() and type casting return the same result for both positive and negative floating-point numbers.

#include <stdio.h>
#include <math.h>
int main()
{
double m = 100.7;
double n = 100.2;
double o = -100.7;
double p = -100.2;

printf("%d and %.0f\n", (int)m, round(m));
printf("%d and %.0f\n", (int)n, round(n));
printf("%d and %.0f\n", (int)o, round(o));
printf("%d and %.0f\n", (int)p, round(p));
return 0;
}
100 and 101
100 and 100
-100 and -101
-100 and -100

Real-time examples

C round to 2 decimal places

The following function rounds the given floating-point number to two decimal places. To compare it with the regular result, we show the round() function result.

#include <stdio.h>
#include <math.h>
double twoDecimals(double n)
{
return round(n * 100.0) / 100.0;
}
int main()
{
double x = 25.6760;
printf("%.2f", twoDecimals(x));
printf("\n%.2f", round(x));
return 0;
}
25.68
26.00

round float to integer

#include <stdio.h>
#include <math.h>
int main()
{
double n = 120.756;
int res = (int)round(n);

printf("Final Result: %d\n", res);
return 0;
}
121