I want to multiply 281.65 by 100 and get 28165, I execute:
fun main(args: Array<String>) {
println("${281.65 * 100}")
}
but I get 28164.999999999996
Whats the problem here and how do I get 28165 as a result?
Is there a good Kotlin way to work with that?
Yep, it's floating point math issue.
kotlin.math package has roundToLong extension function which helps in this case.
(281.65 * 100).roundToLong()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With