Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Rounds a floating-point value to the nearest integer value.
Syntax
double round(
double x
);
float round(
float x
); // C++ only
long double round(
long double x
); // C++ only
float roundf(
float x
);
long double roundl(
long double x
);
#define round(X) // Requires C11 or later
Parameters
x
The floating-point value to round.
Return value
The round functions return a floating-point value that represents the nearest integer to x. Halfway values are rounded away from zero, regardless of the setting of the floating-point rounding mode. There's no error return.
| Input | SEH exception | _matherr exception |
|---|---|---|
| ± QNaN, IND | none | _DOMAIN |
Remarks
Because C++ allows overloading, you can call overloads of round that take and return float and long double values. In a C program, unless you're using the <tgmath.h> macro to call this function, round always takes and returns a double.
If you use the round macro from <tgmath.h>, the type of the argument determines which version of the function is selected. See Type-generic math for details.
By default, this function's global state is scoped to the application. To change this behavior, see Global state in the CRT.
Requirements
| Routine | Required header |
|---|---|
round, roundf, roundl |
<math.h> |
round macro |
<tgmath.h> |
For more compatibility information, see Compatibility.
Example
// Build with: cl /W3 /Tc
// This example displays the rounded
// results of floating-point values
#include <math.h>
#include <stdio.h>
int main()
{
printf("===== Round a float\n\n");
float floatValue = 2.4999999f; // float stores a value close to, but not exactly equal to, the initializer below. floatValue will contain 2.5 because it is the closest single precision value
printf("roundf(%.1000g) is %.1000g\n", floatValue, roundf(floatValue));
printf("roundf(%.1000g) is %.1000g\n", -floatValue, roundf(-floatValue));
// double stores a value close to, but not exactly equal to, the initializer below. The closest double value is just slightly larger.
double doubleValue = 2.4999999;
printf("\n===== Round a double\n\n");
printf("round(%.1000g) is %.1000g\n", doubleValue, round(doubleValue));
printf("round(%.1000g) is %.1000g\n", -doubleValue, round(-doubleValue));
// long double stores a value close to, but not exactly equal to, the initializer below. The closest long double value is just slightly larger.
long double longDoubleValue = 2.4999999L;
printf("\n===== Round a long double\n\n");
printf("roundl(%.1000g) is %.1000g\n", longDoubleValue, roundl(longDoubleValue));
printf("roundl(%.1000g) is %.1000g\n", -longDoubleValue, roundl(-longDoubleValue));
return 0;
}
===== Round a float
roundf(2.5) is 3
roundf(-2.5) is -3
===== Round a double
round(2.499999900000000163657887242152355611324310302734375) is 2
round(-2.499999900000000163657887242152355611324310302734375) is -2
===== Round a long double
roundl(2.499999900000000163657887242152355611324310302734375) is 2
roundl(-2.499999900000000163657887242152355611324310302734375) is -2
See also
Math and floating-point support
ceil, ceilf, ceill
floor, floorf, floorl
fmod, fmodf
lrint, lrintf, lrintl, llrint, llrintf, llrintl
lround, lroundf, lroundl, llround, llroundf, llroundl
nearbyint, nearbyintf, nearbyintl
rint, rintf, rintl