Home > OS >  Bigdecimal operation
Bigdecimal operation

Time:07-26

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
}

CodePudding user response:

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.

  • Related