I want to perform a ceiling function on a number (33.1504352455) so that it returns 33.16. When using ceiling, of course, it returns 34.0. How would I shift the character that the ceiling is acting on so that it returns 33.16?
Asked
Active
Viewed 58 times
2 Answers
2
You could try
number = Math.ceil(oldnumber * 100) / 100.0;
But this could be subject to the vagaries of floating point math.

ControlAltDel
- 33,923
- 10
- 53
- 80
2
For better precision, always opt for BigDecimal. You could do it like:
BigDecimal b = new BigDecimal(33.1504352455);
b = b.setScale(2, RoundingMode.CEILING)
System.out.println(b);

SMA
- 36,381
- 8
- 49
- 73