To round up at 0.09
, and round down at 0.089999999...
, you add 0.01
and truncate to 1 fractional digit.
Truncating positive floating point values can be done using the Math.floor()
method, like this:
Math.floor(value * 10 + 0.1) / 10
UPDATE
Since a number like 0.59
is actually 0.58999999999999996
, it gets rounded down, when the intent is to round up.
One way to get around "broken floating point math", is to use BigDecimal
:
BigDecimal.valueOf(value).add(new BigDecimal("0.01")).setScale(1, RoundingMode.DOWN)
If you do this a lot, the new BigDecimal("0.01")
value should be created as a static final constant.
Note: As with the first solution, this only works for positive numbers.