## Definition of C++ round

C++ round function is defined as a mathematical function returns the integral value or in layman terms, this function is used to round off any given data type like double, float except string type to the nearest integral value. This round() is defined under the cmath.h header file.So, in C++ both round() and std::round() is used for the implementation. Round() helps to solve the applications which have confusion between fractions and decimals and also gives hands-on to speed up the process.

**Syntax:**

The general syntax would be like.

`Return_datatype round(data_type variable);`

Here the parameter is the number to be rounded off with halfway cases.

The data type is it returns the type of the number to be rounded.

### How does round Function work in C++?

A round() function works mainly with an Argument value and it is a Static instance method, the value returned is the nearest int value which is originally assigned as float=3.3; So, the nearest value returned should be 3.0, not 3. Let’s talk Data type Float, a number that has decimal point in it. This Floating-point number is often used in Computer architecture – In taking the binary value manipulation and in any scientific calculation which often has analog and continuous values which is greater than integers. For example, 4.785112 which are stored as bytes. To do precision on this type round() is used to work well.

Let’s take a real-time scenario to understand this function. Suppose we want to round the count to the nearest integer of 10 or million decimal like in 2020 the WHO determined that the population of India was 12,345,234. And the number of people who are affected with COVID-19 is 9,546,321 So this number is difficult to remember therefore the value argument could be rounded out as 9crore.

#### Example

**Code:**

```
double round (double x);
float roundf (float x);
long double roundl (long double x);
round (6.8) = 7
round (-2,2) = -2
```

If the decimal value varies from the range “0-1 -0.5, it returns the result value less than the original value, if the Decimal value is from 0.6 to 0.9 it returns the result of integer value greater than the value. The default value of any decimal integer is to be Zero. Some special values like infinity and +/- 0 returns the same value. The other family of round() functions is roundf () – rounding floating-point numbers and roundl ()- rounding Very long integer.

This calculation is made simplified by roundf () function.

```
float a= 5.9;
int b = a + 0.5; // 6
or use the round() function:
float a = 5.9;
int b = round(a);
```

### Examples of C++ round

In this section, we shall see the demonstration working of the round() function in C++. Let’s get started with simple examples.

#### Example #1 – Using Simple Type ’double ‘

**Code:**

```
#include<bits/stdc++.h>
using namespace std;
int main ()
{
double m = 2.311, n = 4.501, l = 1.412;
cout<< round(m) <<endl;
cout<< round(n) <<endl;
cout<< round(l) <<endl;
double f1 = -2.311, f2 = -1.860, f3 = -2.001;
cout<< round(f1) <<endl;
cout<< round(f2) <<endl;
cout<< round(f3) <<endl;
return 0;
}
```

**Output:**

**Explanation: **This simple code rounds the value of f1,f2, and f3 and their return type is double.

#### Example #2 – Using round()family’s function

**Code:**

```
#include <cmath>
#include <iostream>
using namespace std;
int main ()
{
double m = 11.8, n = 15.3, q = 16.5;
cout<< "Closest value of m :" << round(m) << "\n";
cout<< "Closest value of n :" << round(n) << "\n";
cout<< "Closest value of q :" << round(q) << "\n";
cout<< "lround(-1.0) = " <<lround(-1.0) << "\n";
cout<< "lround(3.2) = " <<lround(3.5) << "\n";
cout<< "lround(5.5) = " <<lround(5.5) << "\n";
cout<< "lround(5.7) = " <<lround(5.7) << "\n";
cout<< "lround(-5.3) = " <<lround(-5.3) << "\n";
cout<< "lround(-6.5) = " <<lround(-6.5) << "\n";
cout<< "lround(-3.7) = " <<lround(-3.7) << "\n";
cout<< "llround(-0.10257) = " <<llround(-0.10257) << "\n";
cout<< "llround(4.3453) = " <<llround(4.3453) << "\n";
cout<< "llround(3.357) = " <<llround(3.357) << "\n";
cout<< "llround(6.6898) = " <<llround(6.6898) << "\n";
cout<< "llround(-4.311) = " <<llround(-4.311) << "\n";
cout<< "llround(-4.4118) = " <<llround(-4.4118) << "\n";
cout<< "llround(-4.4116) = " <<llround(-4.4116) << "\n";
return 0;
}
```

**Output:**

**Explanation: **In the above code, we have manipulated accurately the nearest integral value for the data type double and float as well.

#### Example #3

**Code:**

```
#include <cmath>
#include <iostream>
using namespace std;
intmain()
{
long int x1 = 16, y1 = 40;
double x2 = .16, y2 = .40;
long int res1 = (x1 * y1);
double res2 = (x2 * y2);
cout<< "For the first case: " << round(res1) << "\n";
cout<< "For the Second case: " << round(res2) << "\n";
return 0;
}
```

**Explanation: **When the round function is applied for the integer part it returns the same number as input whereas when it is applied to double or float it gives the rounded value as second output like ‘0’.

**Output:**

#### Example #4 – Using roundf() function

**Code:**

```
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
cout<<roundf(-0.2) <<endl;
cout<<roundf(-0.3) <<endl;
cout<<roundf(-0.4) <<endl;
cout<<roundf(-0.11) <<endl;
cout<<roundf(-0.12) <<endl;
cout<<roundf(0.5) <<endl;
cout<<roundf(0.8) <<endl;
cout<<roundf(0.7) <<endl;
cout<<roundf(0.6) <<endl;
return 0;
}
```

**Output:**

**Explanation: **The above code uses roundf () function to round off the decimal point value with negative arguments. Therefore, the output looks like this:

### Conclusion

Therefore, to conclude, this article along with working and example on the round() helps to learn how to prevent integer overflow. This function helps in mathematical applications in C++. The above-mentioned code is a few functions that are used importantly in a complicated program in retrieving Big Values. The core advantage of using this function is for easy calculation and to get a perfect estimate of an expected result.

### Recommended Articles

This is a guide to C++ round. Here we also discuss the definition and how does the round function work in c++? along with different examples and its code implementation. You may also have a look at the following articles to learn more –