Skip to content
Tutorials Center
My learning

Topics

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.

C
#include <math.h>

double floor(double x);
double ceil(double x);
double trunc(double x);
double round(double x);

Try it

C The four roundings side by side

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.

C Rounding to decimals and to multiples

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

C
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. floor gives 2 and -3, ceil gives 3 and -2, trunc gives 2 and -2, round gives 3 and -3.
  • round is "half away from zero," not the banker's rounding of nearbyint or rint, which round halves to the nearest even number under the default mode. See lround and nearbyint.
  • Casting to int truncates. (int)x equals trunc(x) for values in range. For negative numbers that is different from floor.
  • Rounding to decimals. Scale, round, unscale, as in round_to. The result is the closest double to the decimal value, which still may not print exactly: 0.1 and friends have no exact binary form. Round for display with the printf precision (%.2f) rather than by repeated calls.
  • Money. Do not keep prices as double; use integer cents and round once, when converting.
  • Large values. Every double above 2^52 is already a whole number, so these functions return it unchanged.
  • Ceiling division for integers. For positive ints, (a + b - 1) / b avoids floating point entirely.
  • Variants. floorf, ceilf, truncf, roundf for float; l suffix for long double. With <tgmath.h> the plain names select by type.
  • Standard version. floor and ceil are in C89; round, trunc and the f/l variants arrived in C99.