I want to approximate a number showing only two digits after the decimal point. I wrote this:
String result = String.valueOf(new BigDecimal(price).setScale(4, BigDecimal.ROUND_UP).doubleValue());
If price
is a decimal number like 63,9222
, it works fine. But If price
is ,for example, equal to 7.00
, is printed 7.0
.
I tried to use RoundingMode.CEILING
instead of BigDecimal.ROUND_UP
, but it didn't work and i don't know why. Maybe it will be better to use Double instead of BigDecimal?
Use String.format
double myValue = new BigDecimal(price).setScale(4, BigDecimal.ROUND_UP).doubleValue());
String result = String.format(Locale.ENGLISH, "%.2f", myValue);