Rainmaker
Rainmaker

Reputation: 11100

Kotlin gives wrong result when multipliyng

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?

Upvotes: 4

Views: 464

Answers (1)

Rainmaker
Rainmaker

Reputation: 11100

Yep, it's floating point math issue.

kotlin.math package has roundToLong extension function which helps in this case.

(281.65 * 100).roundToLong()

Upvotes: 8

Related Questions