I've already looked at this asnwer.
I was a little confused by the behaviour of BigDecimal.valueOf(float)
. Here's the example:
System.out.println(BigDecimal.valueOf(20.2f)); //prints 20.200000762939453
Therefore
float f = BigDecimal.valueOf(20.2f)
.setScale(2, RoundingMode.UP)
.floatValue();
System.out.println(f); //prints 20.21
Which is defenitely not the behaviour I expected. Is there a way to round up
float correctly avoiding such errors?