I'm new in C++.I have double variable `double a=0.1239857`

I want to limit variable `a`

from decimal point two digits.So `a`

will be `0.12`

. I know C++ have functions that return largest or smallest integer that is greater or lower than `a`

like ceil or floor.Is there a function that implement digit limitation of floating-point variable? Or How can i change precision of `a`

variable?

Best regards...

Are you actually trying to round the number, or just change its displayed precision?

For the former (truncating the extra digits):

```
double scale = 0.01; // i.e. round to nearest one-hundreth
value = (int)(value / scale) * scale;
```

or (rounding up/down as appropriate, per jheriko's answer)

```
double scale = 0.01; // i.e. round to nearest one-hundreth
value = floor(value / scale + 0.5) * scale;
```

For the latter:

```
cout << setprecision(2) << value;
```

where the parameter to `setprecision()`

is the maximum number of digits to show after the decimal point.

Licensed under: CC-BY-SA with attribution

Not affiliated with: Stack Overflow