C nearbyint() Function

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

Definition

In C, nearbyint() rounds a double to an integer value using the current rounding mode, and returns that value as a double. Unlike rint(), it does not raise the FE_INEXACT floating-point exception when the value changes.

You pass one double. The result is still a double that holds an integer value. Include <math.h>. On GCC, link with -lm.

Choose nearbyint when you want quiet rounding for display or indexing math and you do not want exception flags to fire for ordinary inexact results.

Syntax

Pass the floating-point number. Start with this call.

C
nearbyint(2.5);
CallWhat it does
nearbyint(2.5)Rounds with the current mode and returns a double, without raising FE_INEXACT.

Include <math.h>. On GCC, link with -lm. The return type is double, not long.

Library declarations
C
double nearbyint(double x);
float nearbyintf(float x);
long double nearbyintl(long double x);

Parameters

ParameterDescription
xRequired. The double to round according to the current rounding mode.

Return Value

Return ValueDescription
doubleAn integer-valued double produced by the current rounding mode.

C Examples

Example 1 — Basic Rounding

Round a few values and keep the results as doubles.

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

int main(void) {
    printf("%.1f\n", nearbyint(3.2));
    printf("%.1f\n", nearbyint(3.8));
    printf("%.1f\n", nearbyint(-1.2));
    return 0;
}

How It Works

  1. Include math.h. That declares nearbyint.
  2. Round down-ish. 3.2 becomes 3.0.
  3. Round up-ish. 3.8 becomes 4.0.
  4. Still a double. Notice the .0 in the printed form.

Example 2 — Quiet Versus rint Idea

Use nearbyint when you only need the rounded value, not exception noise.

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

int main(void) {
    double x = 9.75;
    double y = nearbyint(x);

    printf("rounded = %.2f\n", y);
    return 0;
}

How It Works

  1. Start fractional. 9.75 is not an integer.
  2. Call nearbyint. Default mode rounds to the nearest value.
  3. No FE_INEXACT. That is the special quiet behavior.
  4. Keep as double. Handy before formatting for display.

Example 3 — Snap to Whole Numbers

Snap sensor readings onto a whole-number grid.

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

int main(void) {
    double reading = 21.6;
    double snapped = nearbyint(reading);

    printf("snapped = %.0f\n", snapped);
    return 0;
}

How It Works

  1. Read a sample. Real sensors often give fractions.
  2. Snap with nearbyint. Map onto the nearest whole value.
  3. Print cleanly. Whole numbers look tidy in logs.
  4. Mode aware. If you change rounding mode, snapping follows it.

Frequently Asked Questions

nearbyint() rounds a double using the current rounding mode and returns a double without raising FE_INEXACT.
x is required. It is the double to round.
A double that holds the rounded integer value.
Include <math.h>. On GCC, link with -lm.
Both round the same way for values, but nearbyint() does not raise FE_INEXACT.

Next: nextafter()

Find the next representable floating-point value toward another value.

nextafter() 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