0

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?

  • Possible duplicate of [How to round a number to n decimal places in Java](http://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java) – Gerard Roche Sep 23 '16 at 17:24

2 Answers2

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