Which method is used in Kotlin's Double.toInt(), rounding or truncation?
The KDoc of Double.toInt()
is simply inherited from Number.toInt()
, and for that, the exact meaning is, it is defined in the concrete Number
implementation how it is converted to Int
.
In Kotlin, the Double
operations follow the IEEE 754 standard, and the semantics of the Double.toInt()
conversion is the same as that of casting double
to int
in Java, i.e. normal numbers are rounded toward zero, dropping the fractional part:
println(1.1.toInt()) // 1
println(1.7.toInt()) // 1
println(-2.3.toInt()) // -2
println(-2.9.toInt()) // -2