Name

ceil

Synopsis

Rounds a real number up to an integer value

#include <math.h>
doubleceil( double x );
float ceilf( float x );        (C99)
long double ceill( long double x );        (C99)

The ceil() function returns the smallest integer that is greater than or equal to its argument. However, the function does not have an integer type; it returns an integer value, but with a floating-point type.

Example

/* Amount due = unit price * count * VAT, rounded up to the next cent */
div_t total = { 0, 0 };
int count = 17;
int price = 9999;           // 9999 cents is $99.99
double vat_rate = 0.055;    // Value-added tax of 5.5%

total = div( (int)ceil( (count * price) * (1 + vat_rate)), 100);

printf("Total due: $%d.%2d\n", total.quot, total.rem);

This code produces the following output:

Total due: $1793.33

See Also

floor(), floorf(), and floorl(), round(), roundf(), and roundl(); the C99 rounding functions that return floating-point types: trunc(), rint(), nearbyint(), nextafter(), nexttoward(); the C99 rounding functions that return integer types: lrint(), lround(), llrint(), llround(); the fesetround() and fegetround() functions, which operate on the C99 floating-point environment.

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.