C Math round() Function

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
<math.h>

What You’ll Learn

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.

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.

📝 Syntax

Standard C declaration:

C
double round(double x);

Related variants

C
float roundf(float x);             /* <math.h> */
long double roundl(long double x); /* <math.h> */

Parameters

  • x — the floating-point value to round.

Return Value

  • Nearest integer to x, as double.
  • round(15.67) returns 16.0.
  • round(15.32) returns 15.0.
  • Halfway cases round away from zero.

Headers and linking

  • #include <math.h>
  • Compile: gcc round.c -std=c11 -o round -lm
  • For round-to-even ties, see nearbyint() / rint().

⚡ Quick Reference

CallMeaningResult
round(15.67)Nearest integer16
round(15.32)Nearest integer15
round(2.5)Halfway → away from 03
round(-2.5)Halfway negative-3
round(7.0)Already whole7
Nearest
round(x)

Closest integer

Down
floor(x)

Toward −∞

Up
ceil(x)

Toward +∞

Truncate
trunc(x)

Toward zero

Examples Gallery

Compile every example with gcc file.c -std=c11 -o out -lm. Rounding shows up everywhere—prices, scores, pixel coordinates, and statistics.

📚 Getting Started

Round a positive decimal to the nearest whole number.

Example 1 — Round 15.67 to Nearest Integer

Classic example from the reference: round(15.67).

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

int main(void) {
    double number = 15.67;
    double rounded = round(number);

    printf("Original: %.2f\n", number);
    printf("Rounded:  %.0f\n", rounded);

    return 0;
}

How It Works

15.67 is closer to 16 than to 15, so round returns 16.0 (printed as 16).

Example 2 — Halfway Values Round Away from Zero

At exactly .5, C rounds up for positives and down for negatives (away from zero).

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

int main(void) {
    printf("round( 2.5) = %.0f\n", round(2.5));
    printf("round(-2.5) = %.0f\n", round(-2.5));
    printf("round( 3.5) = %.0f\n", round(3.5));
    printf("round(-3.5) = %.0f\n", round(-3.5));

    return 0;
}

How It Works

This is not banker's rounding. round(2.5) becomes 3, not 2. For ties-to-even, use nearbyint.

📈 Practical Patterns

Compare rounding modes, format prices, and cast safely.

Example 3 — round() vs floor() vs ceil() vs trunc()

Same input, four different rounding directions.

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

int main(void) {
    double x = -2.3;

    printf("x = %.1f\n", x);
    printf("round(x) = %.1f  (nearest)\n", round(x));
    printf("floor(x) = %.1f  (toward -inf)\n", floor(x));
    printf("ceil(x)  = %.1f  (toward +inf)\n", ceil(x));
    printf("trunc(x) = %.1f  (toward zero)\n", trunc(x));

    return 0;
}

How It Works

For −2.3, nearest is −2. floor goes further negative to −3. Pick the function that matches your business rule.

Example 4 — Round a Price to Whole Dollars

Display-friendly rounding after a calculation.

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

int main(void) {
    double subtotal = 47.50;
    double tax_rate = 0.08;
    double total = subtotal * (1.0 + tax_rate);
    double rounded_total = round(total);

    printf("Subtotal: $%.2f\n", subtotal);
    printf("Total:    $%.2f\n", total);
    printf("Rounded:  $%.0f\n", rounded_total);

    return 0;
}

How It Works

$47.50 × 1.08 = $51.30. Rounding to whole dollars gives $51. For real money, store cents as integers to avoid float drift.

Example 5 — Cast round() Result to int

round returns double; cast when you need a whole-number type.

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

int main(void) {
    double pixels = 127.6;
    int rounded_pixels = (int)round(pixels);

    printf("pixels = %.1f\n", pixels);
    printf("rounded_pixels (int) = %d\n", rounded_pixels);

    return 0;
}

How It Works

Graphics and array indices often need int. Round first, then cast—do not cast truncating behavior unless that is what you want.

🚀 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.

📝 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.

⚡ 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.

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.

💡 Best Practices

✅ Do

  • Use round for nearest-integer behavior
  • Cast to int after rounding when needed
  • Pick floor/ceil when direction matters
  • Check range before casting huge doubles to int
  • Include <math.h> and link -lm

❌ Don’t

  • Assume round uses banker's rounding at .5
  • Confuse round return type with int
  • Use round when you always need floor or ceil
  • Truncate with (int)x when you mean nearest
  • Rely on float money without integer-cent storage

Key Takeaways

Knowledge Unlocked

Five things to remember about round()

Nearest-integer rounding in C, explained simply.

5
Core concepts
📚 02

0.5 ties

Away from 0.

Rule
📈 03

double out

Cast int.

Type
📄 04

vs floor

Direction.

Compare
🔄 05

vs ceil

Direction.

Compare

❓ Frequently Asked Questions

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.

Explore C Math Functions

Continue with sin() or browse the full math function reference.

Math Functions Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful