Search code examples
javabigdecimal

Confused about result of BigDecimal


How come BigDecimal(9105.0).div(BigDecimal(100.toDouble()))=91.0 instead of 91.05. How do I retain the precision in the decimal?


Solution

  • you use the .div method instead of the .divide method

    the kotlin implementation for .div is:

    @kotlin.internal.InlineOnly
    public inline operator fun BigDecimal.div(other: BigDecimal): BigDecimal = this.divide(other, RoundingMode.HALF_EVEN)
    

    as you can see, there is automatically a RoundingMode activated