C Reference ยท ctype.h, math.h and others
C floor(), ceil(), round() and trunc() functions
Reference for C floor(), ceil(), round() and trunc() from math.h: the four ways to round a double to a whole number, negative values and rounding to decimals.
Four functions in <math.h> turn a floating-point number into a whole number, still stored as a double. They differ in which direction they go. floor() rounds toward negative infinity, ceil() toward positive infinity, trunc() toward zero (it cuts off the fractional part), and round() to the nearest integer, with halfway cases rounded away from zero.
The result type stays floating point. If you need an int, convert afterwards, after checking the range, or use lround, which returns a long directly.
#include <math.h> double floor(double x); double ceil(double x); double trunc(double x); double round(double x);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
double floor(double x); float floorf(float x); long double floorl(long double x); double ceil(double x); float ceilf(float x); long double ceill(long double x); double trunc(double x); float truncf(float x); long double truncl(long double x); double round(double x); float roundf(float x); long double roundl(long double x);
| Name | Description |
|---|---|
x |
The value to round. |
Returns: the rounded value as a floating-point number. NaN, infinities and zeros (including the sign of -0.0) are returned unchanged. A value that is already a whole number is returned exactly.
Errors / undefined behavior: none; these functions never raise a range error. Converting the result to an integer type is undefined behavior when the value does not fit.
Usage notes
- Direction table for 2.5 and -2.5.
floorgives 2 and -3,ceilgives 3 and -2,truncgives 2 and -2,roundgives 3 and -3. roundis "half away from zero," not the banker's rounding ofnearbyintorrint, which round halves to the nearest even number under the default mode. Seelroundandnearbyint.- Casting to
inttruncates.(int)xequalstrunc(x)for values in range. For negative numbers that is different fromfloor. - Rounding to decimals. Scale, round, unscale, as in
round_to. The result is the closestdoubleto the decimal value, which still may not print exactly:0.1and friends have no exact binary form. Round for display with theprintfprecision (%.2f) rather than by repeated calls. - Money. Do not keep prices as
double; use integer cents and round once, when converting. - Large values. Every
doubleabove 2^52 is already a whole number, so these functions return it unchanged. - Ceiling division for integers. For positive ints,
(a + b - 1) / bavoids floating point entirely. - Variants.
floorf,ceilf,truncf,roundfforfloat;lsuffix forlong double. With<tgmath.h>the plain names select by type. - Standard version.
floorandceilare in C89;round,truncand thef/lvariants arrived in C99.