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.

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