Name
ceil
Synopsis
Rounds a real number up to an integer value
#include <math.h> doubleceil( doublex); floatceilf( floatx); (C99) long doubleceill( long doublex); (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.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access