nearbyint()

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...

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn C++ on Codecademy