C++ nearbyint()

Christine_Yang's avatar
Published Nov 5, 2022Updated Dec 21, 2022
Contribute to Docs

The nearbyint() function returns the argument rounded to the closest integer as a floating-point value, according to the current rounding method.

  • 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

nearbyint(value)

The value parameter represents a positive or negative floating-point value. The current rounding method, as described by fegetround(), determines which direction the value is rounded. By default, the rounding direction is set as FE_TONEAREST.

Example

The following example returns the nearby integer of 1.5:

#include <iostream>
#include <cmath>
int main() {
double value = 1.50;
int result;
result = std::nearbyint(value);
std::cout << "Nearest value is " << result << "\n";
// Output: Nearest value is 2
}

This will produce the following output:

Nearest value is 2

Codebyte Example

The following example returns the nearby integer of 2.9:

Code
Output
Loading...

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