Udostępnij za pośrednictwem


round, roundf, roundl

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at round, roundf, roundl.

Rounds a floating-point value to the nearest integer.

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  
);  

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 is 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, round always takes and returns a double.

Requirements

Routine Required header
round, roundf, roundl <math.h>

For additional compatibility information, see Compatibility.

Example

// 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  

.NET Framework Equivalent

System::Math::Round

See Also

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