将浮点值舍入为最接近的整数。
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
要舍入的浮点值。
返回值
round 函数返回表示最接近 x 的整数的浮点值。 无论浮点舍入模式的设置如何,中间的值都远离零舍入。 无错误返回。
输入 |
SEH 异常 |
Matherr 异常 |
---|---|---|
± QNAN,IND |
无 |
_DOMAIN |
备注
由于 C++ 允许重载,您可以调用 round 的重载,该重载采用和返回 float 和 long double 值. 在 C 程序中,round 始终采用并返回 double。
要求
例程 |
必需的标头 |
---|---|
round, roundf, roundl |
<math.h> |
有关其他兼容性信息,请参见兼容性。
示例
// 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));
}
.NET Framework 等效项
请参见
参考
lround、lroundf、lroundl、llround、llroundf、llroundl