Name

lround

Synopsis

Rounds a floating-point number to an integer

#include <math.h>
longlround( double x );
long lroundf( float x );
long lroundl( long double x );

The lround() functions are like round(), except that they return an integer of type long. lround() rounds a floating-point number to the nearest integer value. A number halfway between two integers is rounded away from 0. If the result is outside the range of long, a range error may occur (depending on the implementation), and the return value is unspecified.

Example

long costnow;         // Total cost in cents.
long realcost;
double rate;          // Annual interest rate.
int period;           // Time to defray cost.

/* ... obtain the interest rate to use for calculation ... */

realcost =lround( (double)costnow * exp( rate * (double)period ));

printf( "Financed over %d years, the real cost will be $%ld.%2ld.\n",
        period, realcost/100, realcost % 100 );

Get C in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.