# 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 doubleround(double x); floatround(float x); long doubleround(long double x); doubleround(T x); // additional overloads for integral typesReturns:The value of x rounded to the nearest integral (as a floating-point value).

`// C++ code to demonstrate the ` `// use of round() function ` `#include <cmath> ` `#include <iostream> ` `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; ` `} ` |

*chevron_right*

*filter_none*

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**

**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.**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 <cmath>`

`#include <iostream>`

`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;`

`}`

*chevron_right**filter_none*Output:

From first multiplication :750 From second multiplication :0

**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.**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.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Precision of floating point numbers in C++ (floor(), ceil(), trunc(), round() and setprecision())
- Difference between cout and puts() in C++ with Examples
- Difference between forward list and list in C++
- Short-Circuiting in C++ and Linux
- Different types of range-based for loop iterators in C++
- Learn C++ Programming Step by Step - A 20 Day Curriculum!
- Check if Array elements can be maximized upto M by adding all elements from another array
- Why do we need reference variables if we have pointers
- Similarities and Difference between Java and C++
- Macros and its types in C/C++
- Reversed Range-based for loop in C++ with Examples
- How to find index of a given element in a Vector in C++
- Print all lexicographical greater permutations of a given string
- Introduction to Complex Objects and Composition

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.