Search code examples
kotlinbigdecimal

Bigdecimal operation


why does decrement result -0.8? What is the logic?

import java.math.BigDecimal
fun main(){
    var first = BigDecimal("0.2")
    val decrement = --first
    println(decrement) //-0.8
}

Solution

  • The -- operator is added by Kotlin onto BigDecimal: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/java.math.-big-decimal/dec.html

    For completeness -- means reduce the value by 1, which is why 0.2 - 1 = -0.8. -- is normally used with integers, but it seems Kotlin has extended it for BigDecimal too.