C++ Standard Library C++ STL Library

C++ <cmath> - lround() Function

The C++ <cmath> lround() function rounds the specified number to a nearest integral value, rounding halfway cases away from zero, regardless of the current rounding mode and returns it as long int type.

The llround() function is similar to this function, except it returns the result as long long int type.


long int lround (double x);
long int lround (float x);
long int lround (long double x);
long int lround (T x);                   


x Specify a value to round.

Return Value

Returns the value of x which is first rounded to nearby integral value then casted to long int type.


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

#include <iostream>
#include <cmath>

using namespace std;

int main (){

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

The output of the above code will be:

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

❮ C++ <cmath> Library