The Round Function in C

  1. Use the round Function to Round Floating-Point Number to the Nearest Integer and Return Floating-Point Number
  2. Use the lround Function to Round Floating-Point Number to the Nearest Integer and Return Integral Type
  3. Use the ceil Function to Round Floating-Point Number to the Smallest Integral Value Not Less Than Argument

This article will explain several methods of how to use the round function in C.

Use the round Function to Round Floating-Point Number to the Nearest Integer and Return Floating-Point Number

The round function is part of the C standard library math utilities defined in the <math.h> header file. There are three functions in this family - round, roundf and roundl. These functions are for different types of floating-point numbers, and each returns the corresponding type value. Note that source files, including the math header, need to be compiled using the -lm flag to link the library code. In the following example code, we demonstrate the conversions for multiple float literal values and output the results to the console. Notice that round essentially rounds away from the zero. If the integral value, -0, +0, NaN or INFINITY is passed as an argument, the same value is returned.

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

int main(void)
{
    printf("round(+2.3) = %+.1f\n", round(2.3));
    printf("round(+2.5) = %+.1f\n", round(2.5));
    printf("round(+2.7) = %+.1f\n", round(2.7));
    printf("round(-2.3) = %+.1f\n", round(-2.3));
    printf("round(-2.5) = %+.1f\n", round(-2.5));
    printf("round(-2.7) = %+.1f\n", round(-2.7));

    exit(EXIT_SUCCESS);
}

Output:

round(+2.3) = +2.0
round(+2.5) = +3.0
round(+2.7) = +3.0
round(-2.3) = -2.0
round(-2.5) = -3.0
round(-2.7) = -3.0

Use the lround Function to Round Floating-Point Number to the Nearest Integer and Return Integral Type

The lround function, on the other hand, round to the nearest in integer and return the integral value. There are six functions in this family, the half of which returns long int as rounded value and others - long long int. lround similar to the round family rounds the halfway real numbers away from zero.

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

int main(void)
{
    printf("lround(+2.3) = %ld\n", lround(2.3));
    printf("lround(+2.5) = %ld\n", lround(2.5));
    printf("lround(+2.7) = %ld\n", lround(2.7));
    printf("lround(-2.3) = %ld\n", lround(-2.3));
    printf("lround(-2.5) = %ld\n", lround(-2.5));
    printf("lround(-2.7) = %ld\n", lround(-2.7));

    exit(EXIT_SUCCESS);
}

Output:

lround(+2.3) = 2
lround(+2.5) = 3
lround(+2.7) = 3
lround(-2.3) = -2
lround(-2.5) = -3
lround(-2.7) = -3

Use the ceil Function to Round Floating-Point Number to the Smallest Integral Value Not Less Than Argument

Alternatively, the ceil function can be utilized to round the given floating-point number to the smallest integer value that is not less than the argument itself. Similar to the round function, there are three functions in this family too - ceil, ceilf and ceill to be used for float, double and long double types, respectively. Note that we include the + sign in the prinf specifier string, and it automatically displays the corresponding signs for the rounded arguments.

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

int main(void)
{
    printf("ceil(+2.3) = %+.1f\n", ceil(2.3));
    printf("ceil(+2.5) = %+.1f\n", ceil(2.5));
    printf("ceil(+2.7) = %+.1f\n", ceil(2.7));
    printf("ceil(-2.3) = %+.1f\n", ceil(-2.3));
    printf("ceil(-2.5) = %+.1f\n", ceil(-2.5));
    printf("ceil(-2.7) = %+.1f\n", ceil(-2.7));

    exit(EXIT_SUCCESS);
}

Output:

ceil(+2.3) = 3.000000
ceil(+2.5) = 3.000000
ceil(+2.7) = 3.000000
ceil(-2.3) = -2.000000
ceil(-2.5) = -2.000000
ceil(-2.7) = -2.000000
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.