On the official API doc, it says:
以整数形式返回此数字的值,这可能涉及舍入或截断.
我想要截断,但不确定.有人能解释may involve rounding or truncation
的确切含义吗?
p.s.: In my unit test, (1.7).toInt()
was 1, which might involve truncation.
On the official API doc, it says:
以整数形式返回此数字的值,这可能涉及舍入或截断.
我想要截断,但不确定.有人能解释may involve rounding or truncation
的确切含义吗?
p.s.: In my unit test, (1.7).toInt()
was 1, which might involve 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
.
在Kotlin中,Double
运算遵循IEEE 754标准,Double.toInt()
转换的语义与casting double
to int
in Java相同,即正常数向零舍入,舍go 小数部分:
println(1.1.toInt()) // 1
println(1.7.toInt()) // 1
println(-2.3.toInt()) // -2
println(-2.9.toInt()) // -2