lround, lroundf, lroundl, llround, llroundf, llroundl

Rounds a floating-point value to the nearest integer.

long lround( 
   double x 
);
long lround(
   float x
);  // C++ only
long lround(
   long double x
);  // C++ only
long lroundf(
   float x
);
long lroundl(
   long double x
);
long long llround( 
   double x 
);
long long llround(
   float x
);  // C++ only
long long llround(
   long double x
);  // C++ only
long long llroundf(
   float x
);
long long llroundl(
   long double x
);

Parameters

  • x
    The floating-point value to round.

Return Value

The lround and llround functions return the nearest long or long long 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 lround or llround that take and return float and long double values. In a C program, lround and llround always take and return a double.

Requirements

Routine

Required header

lround, lroundf, lroundl, llround, llroundf, llroundl

<math.h>

For additional compatibility information, see Compatibility.

Example

// crt_lround.c
// Build with: cl /W3 /Tc crt_lround.c
// This example displays the rounded results of
// the floating-point values 2.499999, -2.499999, 
// 2.8, -2.8, 3.5 and -3.5.

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

int main( void )
{
   double x = 2.499999;
   float y = 2.8f;
   long double z = 3.5;

   printf("lround(%f) is %d\n", x, lround(x));
   printf("lround(%f) is %d\n", -x, lround(-x));
   printf("lroundf(%f) is %d\n", y, lroundf(y));
   printf("lroundf(%f) is %d\n", -y, lroundf(-y));
   printf("lroundl(%Lf) is %d\n", z, lroundl(z));
   printf("lroundl(%Lf) is %d\n", -z, lroundl(-z));
}
lround(2.499999) is 2
lround(-2.499999) is -2
lroundf(2.800000) is 3
lroundf(-2.800000) is -3
lroundl(2.500000) is 4
lroundl(-2.500000) is -4

.NET Framework Equivalent

System::Math::Round

See Also

Reference

Floating-Point Support

ceil, ceilf, ceill

floor, floorf, floorl

fmod, fmodf

round, roundf, roundl

rint, rintf, rintl

Other Resources

lrint

nearbyint