title | ms.custom | ms.date | ms.reviewer | ms.suite | ms.technology | ms.tgt_pltfrm | ms.topic | apiname | apilocation | apitype | f1_keywords | dev_langs | helpviewer_keywords | ms.assetid | caps.latest.revision | author | ms.author | manager | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
round, roundf, roundl | Microsoft Docs |
11/04/2016 |
|
article |
|
|
DLLExport |
|
|
|
6be90877-193c-4b80-a32b-c3eca33f9c6f |
6 |
corob-msft |
corob |
ghogen |
Rounds a floating-point value to the nearest integer.
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
);
x
The floating-point value to round.
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 is no error return.
Input | SEH Exception | Matherr Exception |
---|---|---|
± QNAN ,IND |
none | _DOMAIN |
Because C++ allows overloading, you can call overloads of round
that take and return float
and long double
values. In a C program, round
always takes and returns a double
.
Routine | Required header |
---|---|
round , roundf , roundl |
<math.h> |
For additional compatibility information, see Compatibility.
// crt_round.c
// Build with: cl /W3 /Tc crt_round.c
// This example displays the rounded results of
// the floating-point values 2.499999, -2.499999,
// 2.8, -2.8, 2.5 and -2.5.
#include <math.h>
#include <stdio.h>
int main( void )
{
double x = 2.499999;
float y = 2.8f;
long double z = 2.5;
printf("round(%f) is %.0f\n", x, round(x));
printf("round(%f) is %.0f\n", -x, round(-x));
printf("roundf(%f) is %.0f\n", y, roundf(y));
printf("roundf(%f) is %.0f\n", -y, roundf(-y));
printf("roundl(%Lf) is %.0Lf\n", z, roundl(z));
printf("roundl(%Lf) is %.0Lf\n", -z, roundl(-z));
}
round(2.499999) is 2
round(-2.499999) is -2
roundf(2.800000) is 3
roundf(-2.800000) is -3
roundl(2.500000) is 3
roundl(-2.500000) is -3
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