round
round to integral value, regardless of rounding direction
You're viewing an older version of this page (#3307). View the current version.
NAME
round, lround, llround
INTERFACE
#include <math.h>
double round ( double x ) long roundl ( long double x ) float roundf ( float x ) long lround ( double x ) long lroundl ( long double x ) long lroundf ( float x ) long llround ( double x ) long llroundl ( long double x ) long llroundf ( float x )
DESCRIPTION
The round functions return the integral value nearest to x rounding half-way cases away from zero, regardless of the current rounding direction.
The lround and llround functions return the integral value nearest to x (rounding half-way cases away from zero, regardless of the current rounding direction) in the return formats specified. If the rounded value is outside the range of the return type, the numeric result is unspecified and the invalid floating-point exception is raised. A range error may occur if the magnitude of x is too large.
Example 1
#include <stdio.h>
#include <math.h>
int main(void)
{
for(double a=12;a<13;a+=0.1)
printf("round of %.1lf is %.1lf\n", a, round(a));
return 0;
}Output:
round of 12.0 is 12.0
round of 12.1 is 12.0
round of 12.2 is 12.0
round of 12.3 is 12.0
round of 12.4 is 12.0
round of 12.5 is 12.0
round of 12.6 is 13.0
round of 12.7 is 13.0
round of 12.8 is 13.0
round of 12.9 is 13.0
round of 13.0 is 13.0SPECIAL VALUES
round ( ±0 ) returns ±0.
round ( ±∞ ) returns ±∞.
The round functions may, but are not required to, raise the inexact floating-point exception for non-integer numeric arguments.
The lround and llround functions need not raise the inexact floating-point exception for non-integer arguments that round to within the range of the return type.
SEE ALSO
STANDARDS
The round , lround , and llround functions conform to ISO/IEC 9899:1999(E).