C++ rint()

Anonymous contributor's avatar
Anonymous contributor
Published Oct 25, 2022Updated Nov 17, 2022
Contribute to Docs

The rint() function rounds the argument to an integral value using the current rounding direction.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
    • Beginner Friendly.
      11 hours

Syntax

rint(x)

The x parameter must be a double, float, or long double. The return value will be the same data type.

Note: The rounding direction is specified by the fegetround() function, with the default direction set as to the nearest. The rounding direction can be set to other values using fesetround() function.

Example

The following example uses the rint() function to find the value of x after rounding off:

#include <iostream>
#include <cmath>
int main() {
double x = 11.87;
double result;
result = rint(x);
std::cout << "Rounding to-nearest integer of (" << x << ") = " << result << "/n";
}

This produces the following output:

Rounding to-nearest integer (11.87) = 12

Codebyte Example

The following example is runnable and returns the value nearest to x:

Code
Output

All contributors

Contribute to Docs

Learn C++ on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
    • Beginner Friendly.
      11 hours