0Pricing
C Academy · Lesson

Rounding and Absolute Values

floor, ceil, round, fabs.

Rounding and Absolute Values

This lesson covers functions that adjust numbers: floor, ceil, round, and trunc for rounding, plus fabs for absolute value.

They all live in math.h and work with double values.

The floor Function

floor(x) rounds down to the nearest whole number, toward negative infinity.

So floor(3.9) is 3.0, and floor(-1.2) is -2.0.

#include <stdio.h>
#include <math.h>

int main(void) {
    printf("%.1f\n", floor(3.9));
    printf("%.1f\n", floor(-1.2));
    return 0;
}

All lessons in this course

  1. Including math.h
  2. Powers and Roots
  3. Trig and Logarithms
  4. Rounding and Absolute Values
← Back to C Academy