Float round up to 2 decimals java

bigdecimal, decimal, floating-point, java, rounding

Solution

Since you are using `BigDecimal.ROUND_HALF_UP`, `29.294998` is rounded to `29.29`. You might want to use `BigDecimal.ROUND_UP` instead.

Check BigDecimal doc for more informations on each rounding available.

Problem

I'm trying to round a float up to 2 decimals. I've 2 float values: ``` 1.985 29.294998 ``` Both of them will need to be rounded up so I end up with the following: ``` 1.99 29.30 ``` When I use the following method: ``` public static Float precision(int decimalPlace, Float d) { BigDecimal bd = new BigDecimal(Float.toString(d)); bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP); return bd.floatValue(); } ``` Te result is: ``` 1.99 29.29 ```

Original source