C++ Standard Library C++ STL Library

C++ <cmath> - round() Function



The C++ <cmath> round() function returns an integral value that is nearest to the argument value, regardless of the current rounding mode. In halfway cases, the argument is rounded away from zero.

Syntax

double round (double x);
float round (float x);
long double round (long double x);
double round (T x);                    

Parameters

x Specify a value to round.

Return Value

Returns an integral value by rounding up the x to the nearest integral value.

Example:

In the example below, round() function is used to round the given number.

#include <iostream>
#include <cmath>

using namespace std;

int main (){

  cout<<"round(10.2): "<<round(10.2)<<"\n";
  cout<<"round(10.8): "<<round(10.8)<<"\n";
  cout<<"round(-5.2): "<<round(-5.2)<<"\n";
  cout<<"round(-5.8): "<<round(-5.8)<<"\n";
  return 0;
}

The output of the above code will be:

round(10.2): 10
round(10.8): 11
round(-5.2): -5
round(-5.8): -6

❮ C++ <cmath> Library

5