Home C Math Functions rint() C rint() Function Definition In C, rint() rounds x to a nearby integer value according to the current floating-point rounding mode. The result is still a floating type (a double), not an int.
You pass one double. Include <math.h>. The default rounding mode on most systems is round-to-nearest, ties to even. Unlike round() , which uses a fixed halfway rule away from zero, rint follows the active mode.
If the result differs from x, an inexact floating-point exception may be raised. For a related function that avoids that exception, see nearbyint when your library provides it.
Syntax Pass the floating-point number. Start with this call.
Call What it does rint(2.5);Rounds 2.5 using the current mode (often to 2.0 with ties-to-even).
Include <math.h>. On GCC, link with -lm. The return type is still floating-point.
Library declarations double rint(double x);
float rintf(float x);
long double rintl(long double x); Parameters Parameter Description x Required. The double to round to a nearby integer value.
Return Value Return Value Description Rounded double An integer-valued double chosen by the current rounding mode.
C Examples 1. Basic rint 2. Ties to even 3. Still a double Example 1 — Basic Rounding Print a few everyday results under the usual default mode.
#include <stdio.h>
#include <math.h>
int main(void) {
printf("rint(2.3) = %.1f\n", rint(2.3));
printf("rint(2.7) = %.1f\n", rint(2.7));
printf("rint(-2.3) = %.1f\n", rint(-2.3));
return 0;
} rint(2.3) = 2.0
rint(2.7) = 3.0
rint(-2.3) = -2.0 How It Works Include math.h. That declares rint.Below halfway. 2.3 goes to 2.0.Above halfway. 2.7 goes to 3.0.Negatives. Rounding still moves toward the nearer integer.Example 2 — Halfway Ties (Default Mode) With the common ties-to-even mode, 2.5 and 3.5 round to even integers.
#include <stdio.h>
#include <math.h>
int main(void) {
printf("rint(2.5) = %.1f\n", rint(2.5));
printf("rint(3.5) = %.1f\n", rint(3.5));
printf("round(2.5) = %.1f\n", round(2.5));
return 0;
} rint(2.5) = 2.0
rint(3.5) = 4.0
round(2.5) = 3.0 How It Works Halfway 2.5. Nearest even integer is 2.Halfway 3.5. Nearest even integer is 4.Compare round. round(2.5) goes away from zero to 3.Know your rule. Pick the API that matches the math you need.Example 3 — Result Type Stays Floating The value looks like an integer, but the type is still double.
#include <stdio.h>
#include <math.h>
int main(void) {
double x = 9.8;
double y = rint(x);
printf("y = %.1f\n", y);
printf("y + 0.25 = %.2f\n", y + 0.25);
return 0;
} y = 10.0
y + 0.25 = 10.25 How It Works Round 9.8. Default mode yields 10.0.Keep as double. No cast to int happened.Add a fraction. Floating math still works.Cast only if needed. Convert explicitly when you want an integer type.Frequently Asked Questions What does rint() do in C? rint(x) rounds x to a nearby integer value using the current floating-point rounding mode and returns that value as a double.
What is the parameter of rint()? x is required. It is the double you want to round.
What does rint() return? A double that is an integer value selected by the active rounding mode.
Which header file does rint() need? Include <math.h>. On GCC, link with -lm.
How is rint() different from round()? round() uses a fixed rule (halfway cases away from zero). rint() follows the current rounding mode, often ties to even by default.
Next: round() Efficiently multiply by FLT_RADIX raised to a long exponent.
round() tutorial → CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
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
Helpful Share Copy link Suggestion