# rounding – C++ round a double up to 2 decimal places

## rounding – C++ round a double up to 2 decimal places

To round a double up to 2 decimal places, you can use:

```
#include <iostream>
#include <cmath>
int main() {
double value = 0.123;
value = std::ceil(value * 100.0) / 100.0;
std::cout << value << std::endl; // prints 0.13
return 0;
}
```

To round up to n decimal places, you can use:

```
double round_up(double value, int decimal_places) {
const double multiplier = std::pow(10.0, decimal_places);
return std::ceil(value * multiplier) / multiplier;
}
```

This method wont be particularly fast, if performance becomes an issue you may need another solution.

If it is just a matter of writing to screen then to round the number use

```
std::cout.precision(3);
std::cout << gpa << std::endl;
```

floating points are not exactly represented so by internally rounding the value and then using that in your calculations you are increasing the inexactness.

#### rounding – C++ round a double up to 2 decimal places

Try this. But your `cout`

statement in else condition, so it wont give the desired output for 3.67924.

```
if (cin >> gpa)
{
if (gpa >= 0 && gpa <= 5) {
// valid number
gpa = ceil(gpa * 100);
gpa=gpa/100;
break;
}
else
{
cout << Please enter a valid GPA (0.00 - 5.00) << endl;
cout << GPA: ;
}
}
```