C round() Function

Definition
In C, round() rounds x to the nearest integer. A halfway value moves away from zero, so round(1.5) is 2 and round(-1.5) is -2.
You pass one double. The result is still a double, such as 2.0, not an int. Include <math.h>.
Syntax
Pass the number you want to round. Start with this call.
| Call | What it does |
|---|
round(1.5) | Rounds halfway away from zero and returns 2. |
Include <math.h>. On GCC, link with -lm. The result type is still double.
Library declarations
double round(double x);
float roundf(float x);
long double roundl(long double x);
Parameters
| Parameter | Description |
|---|
| x | Required. The double to round to the nearest integer. |
Return Value
| Return Value | Description |
|---|
| double | The nearest integer value, stored as a double. Halfway cases go away from zero. |
C Examples
Example 1 — Round to the Nearest Integer
Print a few everyday rounding results.
#include <math.h>
#include <stdio.h>
int main(void) {
printf("round(15.67) = %.1f\n", round(15.67));
printf("round(15.32) = %.1f\n", round(15.32));
printf("round(-2.3) = %.1f\n", round(-2.3));
return 0;
}
round(15.67) = 16.0
round(15.32) = 15.0
round(-2.3) = -2.0
How It Works
- Include math.h. Declares
round; link with -lm on GCC. - Above .5.
15.67 is closer to 16. - Below .5.
15.32 is closer to 15. - Negatives.
-2.3 is closer to -2 than to -3.
Example 2 — Halfway Ties Away from Zero
Exact .5 values round away from zero in C (round), unlike trunc.
#include <math.h>
#include <stdio.h>
int main(void) {
printf("round(2.5) = %.1f\n", round(2.5));
printf("round(-2.5) = %.1f\n", round(-2.5));
printf("trunc(2.5) = %.1f\n", trunc(2.5));
return 0;
}
round(2.5) = 3.0
round(-2.5) = -3.0
trunc(2.5) = 2.0
How It Works
- Positive half.
2.5 is tied → pick 3 (away from 0). - Negative half.
-2.5 is tied → pick -3 (also away from 0). - Not trunc.
trunc would keep 2.0 by dropping the fraction. - Still a double. Printed values look like
3.0, not an int.
Example 3 — Round a Displayed Price
Round a price to the nearest whole currency unit for a simple display.
#include <math.h>
#include <stdio.h>
int main(void) {
double price = 19.49;
double rounded = round(price);
printf("price = %.2f\n", price);
printf("rounded = %.0f\n", rounded);
return 0;
}
price = 19.49
rounded = 19
How It Works
- Start with a price.
19.49 is closer to 19 than to 20. - Call round. Result is
19.0 as a double. - Print cleanly.
%.0f shows a whole number for display. - Money note. Real money often uses integer cents; this shows nearest-unit rounding only.
Frequently Asked Questions
round() rounds x to the nearest integer. Halfway values move away from zero, so round(1.5) is 2 and round(-1.5) is -2.
x is required. It is the double to round.
A double holding an integer value. It is not an int.
Include <math.h>. On GCC, link with -lm.
It rounds away from zero. round(2.5) is 3, and round(-2.5) is -3.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful