nearbyint()

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.

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:

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

All contributors

Looking to contribute?

Learn C++ on Codecademy