C round() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math.h

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.

C
round(1.5);
CallWhat 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
C
double round(double x);
float roundf(float x);
long double roundl(long double x);

Parameters

ParameterDescription
xRequired. The double to round to the nearest integer.

Return Value

Return ValueDescription
doubleThe 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.

C
#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;
}

How It Works

  1. Include math.h. Declares round; link with -lm on GCC.
  2. Above .5. 15.67 is closer to 16.
  3. Below .5. 15.32 is closer to 15.
  4. 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.

C
#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;
}

How It Works

  1. Positive half. 2.5 is tied → pick 3 (away from 0).
  2. Negative half. -2.5 is tied → pick -3 (also away from 0).
  3. Not trunc. trunc would keep 2.0 by dropping the fraction.
  4. 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.

C
#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;
}

How It Works

  1. Start with a price. 19.49 is closer to 19 than to 20.
  2. Call round. Result is 19.0 as a double.
  3. Print cleanly. %.0f shows a whole number for display.
  4. 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.

Next: scalbln()

Compute the sine of an angle in radians.

scalbln() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

9 people found this page helpful