XxTurtlesRkoolXx
XxTurtlesRkoolXx

Reputation: 13

Shifting Math.ceil in Java

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?

Upvotes: 0

Views: 65

Answers (2)

SMA
SMA

Reputation: 37023

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);

Upvotes: 2

ControlAltDel
ControlAltDel

Reputation: 35011

You could try

number = Math.ceil(oldnumber * 100) / 100.0;

But this could be subject to the vagaries of floating point math.

Upvotes: 2

Related Questions