4

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?

Michiel Leegwater
  • 1,172
  • 4
  • 11
  • 27
Rainmaker
  • 10,294
  • 9
  • 54
  • 89
  • 6
    Possible duplicate of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Kyll Sep 24 '19 at 16:52

1 Answers1

8

Yep, it's floating point math issue.

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

(281.65 * 100).roundToLong()
Rainmaker
  • 10,294
  • 9
  • 54
  • 89