Ads by ProfitSence
Close

C++ round()

Last Updated on Wednesday 5th Oct 2022

C++ round

The round() function in C++ returns the integral value that is nearest to x, with halfway cases rounded away from zero

how to round in c++

			
					#include <iostream>
#include <cmath>
using namespace std;
int main() {
  cout << round(14.4);
  cout << round(14.6);
  return 0;
}

			
	
			
					14
15

			
	

round() Syntax

The syntax of round() function is:

			
					round(double num);

			
	

round c++

round() Parameters

The round() function takes the num parameters. * A floating-point number - num - which is to be rounded off , can be a any of double , float , long double.

c++ round() returns

  • The value of num rounded to the nearest integral (as a floating-point value).

rounding in c++

			
					#include <iostream>
#include <cmath>
using namespace std;

int main() {

  double num, res;

  num = 14.14;
  res = round(num);

  cout << "round(" << num << ") = " << res << endl;

  num = 44.88;
  res = round(num);

  cout << "round(" << num << ") = " << res << endl;

  num = 21.34;
  res = round(num);

  cout << "round(" << num << ") = " << res << endl;

  num = 87.56;
  res = round(num);

  cout << "round(" << num << ") = " << res;

  return 0;
}

			
	

std round

			
					round(14.14) = 14
round(44.88) = 45
round(21.34) = 21
round(87.56) = 88

			
	

With Negative Numbers

c++ round to nearest integer

			
					#include <iostream>
#include <cmath>
using namespace std;

int main() {

  double num, res;

  num = -14.14;
  res = round(num);

  cout << "round(" << num << ") = " << res << endl;

  num = -44.88;
  res = round(num);

  cout << "round(" << num << ") = " << res << endl;

  num = -21.34;
  res = round(num);

  cout << "round(" << num << ") = " << res << endl;

  num = -87.56;
  res = round(num);

  cout << "round(" << num << ") = " << res;

  return 0;
}

			
	
			
					round(-14.14) = -14
round(-44.88) = -45
round(-21.34) = -21
round(-87.56) = -88

			
	

References