Rounding and Absolute Values
floor, ceil, round, fabs.
Rounding and Absolute Values is a free C Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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;
}The ceil Function
ceil(x) rounds up to the nearest whole number, toward positive infinity.
So ceil(3.1) is 4.0, and ceil(-1.8) is -1.0.
#include <stdio.h>
#include <math.h>
int main(void) {
printf("%.1f\n", ceil(3.1));
printf("%.1f\n", ceil(-1.8));
return 0;
}The round Function
round(x) rounds to the nearest whole number. Halfway cases round away from zero.
So round(2.5) is 3.0 and round(2.4) is 2.0.
#include <stdio.h>
#include <math.h>
int main(void) {
printf("%.1f\n", round(2.5));
printf("%.1f\n", round(2.4));
return 0;
}The trunc Function
trunc(x) simply chops off the decimal part, always moving toward zero.
So trunc(3.9) is 3.0 and trunc(-3.9) is -3.0.
#include <stdio.h>
#include <math.h>
int main(void) {
printf("%.1f\n", trunc(3.9));
printf("%.1f\n", trunc(-3.9));
return 0;
}Comparing the Four
For the value 2.7 the results differ:
floor(2.7)= 2.0 (down)ceil(2.7)= 3.0 (up)round(2.7)= 3.0 (nearest)trunc(2.7)= 2.0 (toward zero)
Choose the one that matches your intent.
Absolute Value with fabs
fabs(x) returns the absolute value of a floating-point number: the distance from zero, always non-negative.
So fabs(-7.5) is 7.5.
#include <stdio.h>
#include <math.h>
int main(void) {
printf("%.1f\n", fabs(-7.5));
printf("%.1f\n", fabs(7.5));
return 0;
}fabs versus abs
Be careful: abs (from stdlib.h) works on integers, while fabs (from math.h) works on doubles.
Using abs on a double would chop off the decimals first. For floating-point, always use fabs.
Converting to int
These functions return a double. To store a whole number as an int, cast the result.
It is safest to round first, then cast, so values like 2.999 do not become 2 by accident.
#include <stdio.h>
#include <math.h>
int main(void) {
double x = 4.6;
int whole = (int)round(x);
printf("whole = %d\n", whole);
return 0;
}A Distance Example
fabs is handy for measuring how far apart two values are, no matter which is larger.
Here we find the gap between two temperatures.
#include <stdio.h>
#include <math.h>
int main(void) {
double a = 5.0, b = 12.5;
printf("gap = %.1f\n", fabs(a - b));
return 0;
}Rounding to Decimals
To round to two decimal places, multiply by 100, round, then divide by 100.
This trick uses round to keep money-style values tidy.
#include <stdio.h>
#include <math.h>
int main(void) {
double price = 3.14159;
double r = round(price * 100.0) / 100.0;
printf("%.2f\n", r);
return 0;
}Quick Check
Recall how each rounding function behaves.
Recap
You learned floor (down), ceil (up), round (nearest), and trunc (toward zero), plus fabs for absolute value of doubles.
Remember to use fabs instead of abs for floating-point, and cast to int when you need a whole number. Great work finishing the math.h course!
Frequently asked questions
Is the “Rounding and Absolute Values” lesson free?
Yes — the full text of “Rounding and Absolute Values” is free to read here on the web, and the C Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO.
What will I learn in “Rounding and Absolute Values”?
floor, ceil, round, fabs. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C Academy?
No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Rounding and Absolute Values” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C Academy lesson?
Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Including math.h
- Powers and Roots
- Trig and Logarithms
- Rounding and Absolute Values