Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

round — rounding to the nearest integer value

For a finite input, round from <math.h> returns the nearest integer value as a double. Exact halfway cases are rounded away from zero.

Version: C23; repository compiler flag, production runtime not attested

Syntax

#include <math.h>
double round(double x);

round(x) takes a double and returns a double, not an integer-type object. Its declaration is provided by <math.h>.

For a finite input, it returns the nearest integer value. Exact halfway cases round away from zero: 2.5 produces 3.0, and -2.5 produces -3.0. The current floating-point rounding direction does not affect the result.

On IEC 60559 implementations, round returns positive or negative infinity unchanged and returns NaN for a NaN input.

Do not confuse this operation with truncation using trunc or ties-to-even rounding using roundeven. The variants roundf and roundl handle float and long double, respectively.

Examples

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

int main(void)
{
    printf("%.0f %.0f %.0f\n", round(2.5), round(-2.5), round(2.25));
    return 0;
}

Result

3 -3 2

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources