How to use round() function to round to nearest integer in floating-point format, halfway cases away from zero in C++

1 Answer

0 votes
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
	cout << "round(3.3) = " << round(3.3) << endl;
	cout << "round(3.5) = " << round(3.5) << endl;
	cout << "round(4.5) = " << round(4.5) << endl;
	cout << "round(4.6) = " << round(4.6) << endl;
	cout << "round(-3.3) = " << round(-3.3) << endl;
	cout << "round(-3.5) = " << round(-3.5) << endl;
	cout << "round(-4.5) = " << round(-4.5) << endl;
	cout << "round(-4.6) = " << round(-4.6) << endl;

	return 0;
}

/*
run:

round(3.3) = 3
round(3.5) = 4
round(4.5) = 5
round(4.6) = 5
round(-3.3) = -3
round(-3.5) = -4
round(-4.5) = -5
round(-4.6) = -5

*/

 



answered Mar 29, 2016 by avibootz
...