I'm trying to get the total amount of double values but it returns unexpected results. Check the following screenshot:
Link for the code Code:
import java.math.BigDecimal
/**
* You can edit, run, and share this code.
* play.kotlinlang.org
*/
fun main() {
val items = listOf(MyItem(BigDecimal(3.6)), MyItem(BigDecimal(2.0)), MyItem(BigDecimal(4.3)), MyItem(BigDecimal(0.1)))
println(items.sumOf { it.amount })
}
data class MyItem(val amount: BigDecimal)
The expected result is: 10.0
The actual result is: 9.9999999999999999167332731531132594682276248931884765625