The round() function rounds a floating-point number to the nearest integer. Values exactly halfway between two integers round away from zero. Use it when you want classic “closest whole number” behavior, not always-down or always-up rounding.
01
Nearest
Integer.
02
math.h
Link -lm.
03
0.5 ties
Away from 0.
04
vs floor
Always down.
05
vs ceil
Always up.
06
double out
Cast to int.
Fundamentals
Definition and Usage
round(x) picks the integer closest to x. If x is already whole, it returns unchanged (as a double, e.g. 5.0).
For ties at exactly .5, C rounds away from zero: round(2.5) = 3.0 and round(-2.5) = -3.0. This is not banker's rounding (round-to-even).
💡
Beginner Tip
The return type is double, not int. After round, cast explicitly: int n = (int)round(x);. For “always round down” use floor; for “always round up” use ceil.
Graphics and array indices often need int. Round first, then cast—do not cast truncating behavior unless that is what you want.
Applications
🚀 Common Use Cases
Display formatting — show whole numbers from calculated decimals.
Pixel coordinates — snap sub-pixel positions to nearest pixel.
Statistics — report rounded averages or percentages.
Game scores — nearest-integer health, damage, or timers.
Survey results — round response averages for charts.
🧠 How round() Works
1
Receive x
Any finite double value.
Input
2
Find nearest integer
Pick closest whole number; ties go away from zero.
Compute
3
Return as double
e.g. 16.0, not 16 as int.
Output
=
🔢
Nearest integer
Cast to int when your API needs integers.
Important
📝 Notes
Returns double, not int—cast explicitly when needed.
Halfway ties round away from zero (not round-to-even).
round(2.5) = 3; round(-2.5) = -3.
For always-down: floor. For always-up: ceil. For toward zero: trunc.
NaN input → NaN output; ±∞ → ±∞.
Link with -lm on GCC/Clang Unix-like builds.
Performance
⚡ Optimization
round() is fine for most code. If you only need truncation toward zero and the value is positive, (int)x may suffice (with range checks). In tight loops processing millions of values, profile before replacing round—correct rounding rules matter more than micro-seconds in most apps.
Wrap Up
Conclusion
round() rounds floating-point numbers to the nearest integer, with halfway cases going away from zero. It returns double and pairs naturally with casts for integer APIs.
Continue with sin() for trigonometry, or review floor() and ceil() for directional rounding.
round(x) returns the nearest integer to x as a double. For example, round(15.67) is 16.0 and round(15.32) is 15.0. Halfway values like 2.5 round away from zero to 3.0.
Include <math.h> and link with -lm on many systems: gcc program.c -o program -lm. The function is declared as double round(double x).
C round() ties halfway cases away from zero: round(2.5) is 3.0 and round(-2.5) is -3.0. It does NOT use banker's rounding (round-to-even). For ties-to-even, use nearbyint() or rint().
round(x) goes to the nearest integer. floor(x) rounds toward negative infinity (down for positives). ceil(x) rounds toward positive infinity (up for positives). round(2.3)=2, floor(2.3)=2, ceil(2.3)=3.
No. round() returns double (e.g. 16.0). Cast when you need int: int n = (int)round(x); ensure the value fits in int range before casting.
roundf(x) takes float and returns float. roundl(x) takes long double and returns long double. Use the variant matching your variable type.
Did you know?
C offers several rounding functions with different tie-breaking rules. round ties away from zero; nearbyint and rint tie to the nearest even integer (banker’s rounding). Pick the function that matches your specification—they are not interchangeable at exactly .5.