# round() in C++

round is used to round off the given digit which can be in float or double. It returns the nearest integral value to provided parameter in round function, with halfway cases rounded away from zero. Instead of round(), std::round() can also be used .
Header files used -> cmath, ctgmath

Syntax :

Parameters: x, value to be rounded
double round (double x);
float round (float x);
long double round (long double x);
double round (T x);

Returns: The value of x rounded to the nearest
integral (as a floating-point value).

 // C++ code to demonstrate the // use of round() function #include #include using namespace std;    // Driver program int main() {     // initializing value     double x = 12.5, y = 13.3, z = 14.8;        // Displaying the nearest values     // of x, y and z     cout << "Nearest value of x :" << round(x) << "\n";     cout << "Nearest value of y :" << round(y) << "\n";     cout << "Nearest value of z :" << round(z) << "\n";        // For lround     cout << "lround(-0.0) = " << lround(-0.0) << "\n";     cout << "lround(2.3) = " << lround(2.3) << "\n";     cout << "lround(2.5) = " << lround(2.5) << "\n";     cout << "lround(2.7) = " << lround(2.7) << "\n";     cout << "lround(-2.3) = " << lround(-2.3) << "\n";     cout << "lround(-2.5) = " << lround(-2.5) << "\n";     cout << "lround(-2.7) = " << lround(-2.7) << "\n";        // For llround     cout << "llround(-0.01234) = " << llround(-0.01234) << "\n";     cout << "llround(2.3563) = " << llround(2.3563) << "\n";     cout << "llround(2.555) = " << llround(2.555) << "\n";     cout << "llround(2.7896) = " << llround(2.7896) << "\n";     cout << "llround(-2.323) = " << llround(-2.323) << "\n";     cout << "llround(-2.5258) = " << llround(-2.5258) << "\n";     cout << "llround(-2.71236) = " << llround(-2.71236) << "\n";        return 0; }

Output:

Nearest value of x :13
Nearest value of y :13
Nearest value of z :15
lround(-0.0) = 0
lround(2.3) = 2
lround(2.5) = 3
lround(2.7) = 3
lround(-2.3) = -2
lround(-2.5) = -3
lround(-2.7) = -3
llround(-0.01234) = 0
llround(2.3563) = 2
llround(2.555) = 3
llround(2.7896) = 3
llround(-2.323) = -2
llround(-2.5258) = -3
llround(-2.71236) = -3

Here, in the above program we have just calculated the nearest integral value of given float or double value.
which has been calculated accurately.

Possible Applications

1. Handling the mismatch between fractions and decimal : One use of rounding numbers is shorten all the three’s to the right of the decimal point in converting 1/3 to decimal. Most of the time, we will use the rounded numbers 0.33 or 0.333 when we need to work with 1/3 in decimal. We usually work with just two or three digits to the right of the decimal point when there is no exact equivalent to the fraction in decimal.
2. Changing multiplied result : There will be difference between multiplication of 25, 75 and 0.25, 0.75 we get 0.875 .We started with 2 digits to the right of the decimal point and ended up with 4. Many times we will just round up the result to 0.19 .

 // C+++ code for above explanation #include #include using namespace std;    // Driver program int main() {     // Initializing values for int type     long int a1 = 25, b1 = 30;        // Initializing values for double type     double a2 = .25, b2 = .30;     long int ans_1 = (a1 * b1);     double ans_2 = (a2 * b2);        // Rounded result for both     cout << "From first multiplication :" << round(ans_1) << "\n";     cout << "From second multiplication :" << round(ans_2) << "\n";     return 0; }

Output:

From first multiplication :750
From second multiplication :0

3. Fast calculation : Suppose in need of fast calculation we take approx value and then calculate nearest answer. For example, we get an answer 298.78 after any calculation and by rounding off we get an absolute answer of 300.
4. Getting estimate : Sometimes you want to round integers instead of decimal numbers. Usually you are interested in rounding to the nearest multiple of 10, 100, 1, 000 or million. For example, in 2006 the census department determined that the population of New York City was 8, 214, 426. That number is hard to remember and if we say the population of New York City is 8 million it is a good estimate because it doesn’t make any real difference what the exact number is.

Reference : www.mathworksheetcenter.com, www.cplusplus.com

This article is contributed by Himanshu Ranjan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.