``` assertEquals( BigDecimal("0...
# announcements
j
Copy code
assertEquals(
            BigDecimal("0.08333333333333333"),
            (1.toBigDecimal() / 12.toBigDecimal())
        )

expected: <0.08333333333333333> but was: <0>
The bottom evaluates to
0
which is clearly incorrect. What is the rule here and how can I avoid this pitfall?
r
1.toBigDecimal().setScale(17) / 12.toBigDecimal()
j
I’d have to determine a scale beforehand?
r
generally you have to specify the scale of the result - that's what BigDecimal class is all about
Copy code
1.toBigDecimal().divide(12.toBigDecimal(), 17, java.math.BigDecimal.ROUND_HALF_UP)
j
Thank you @Robert Jaros
It looks like this is actually related to Kotlin:
Copy code
@kotlin.internal.InlineOnly
public inline operator fun BigDecimal.div(other: BigDecimal): BigDecimal = this.divide(other, RoundingMode.HALF_EVEN)
The default divide operator uses half even rounding and determines scale from the operating decimals. If neither have a scale, it rounds to the nearest integer. You can override the division operator using:
Copy code
private operator fun BigDecimal.div(other: BigDecimal) = divide(other, MathContext.DECIMAL128)
which changes the behavior to compute the decimal positions.