# 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) << ````" "````; ` `    ``cout << ``"Nearest value of y :"` `<< round(y) << ````" "````; ` `    ``cout << ``"Nearest value of z :"` `<< round(z) << ````" "````; ` ` `  `    ``// For lround ` `    ``cout << ``"lround(-0.0) = "` `<< lround(-0.0) << ````" "````; ` `    ``cout << ``"lround(2.3) = "` `<< lround(2.3) << ````" "````; ` `    ``cout << ``"lround(2.5) = "` `<< lround(2.5) << ````" "````; ` `    ``cout << ``"lround(2.7) = "` `<< lround(2.7) << ````" "````; ` `    ``cout << ``"lround(-2.3) = "` `<< lround(-2.3) << ````" "````; ` `    ``cout << ``"lround(-2.5) = "` `<< lround(-2.5) << ````" "````; ` `    ``cout << ``"lround(-2.7) = "` `<< lround(-2.7) << ````" "````; ` ` `  `    ``// For llround ` `    ``cout << ``"llround(-0.01234) = "` `<< llround(-0.01234) << ````" "````; ` `    ``cout << ``"llround(2.3563) = "` `<< llround(2.3563) << ````" "````; ` `    ``cout << ``"llround(2.555) = "` `<< llround(2.555) << ````" "````; ` `    ``cout << ``"llround(2.7896) = "` `<< llround(2.7896) << ````" "````; ` `    ``cout << ``"llround(-2.323) = "` `<< llround(-2.323) << ````" "````; ` `    ``cout << ``"llround(-2.5258) = "` `<< llround(-2.5258) << ````" "````; ` `    ``cout << ``"llround(-2.71236) = "` `<< llround(-2.71236) << ````" "````; ` ` `  `    ``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) << ````" "````; ` `    ``cout << ``"From second multiplication :"` `<< round(ans_2) << ````" "````; ` `    ``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

## tags:

C++ CPP-Library STL STL CPP