LoginSignup
1
0

More than 1 year has passed since last update.

【Java】【Kotlin/JVM】整数へのキャストでの丸め

Posted at

↑この記事を見て、そういえばよく忘れるなぁと思ったのでまとめておく。


Java と Kotlin/JVM で浮動小数点数を整数にキャストすると
端数は 0 に近づける方向に切り捨てられる。

Java
System.out.println((int) -0.75); // > 0
System.out.println((int) -0.25); // > 0
System.out.println((int) +0.25); // > 0
System.out.println((int) +0.75); // > 0
Kotlin/JVM
println((-0.75).toInt()) // > 0
println((-0.25).toInt()) // > 0
println((+0.25).toInt()) // > 0
println((+0.75).toInt()) // > 0

The Java® Language Specification より

the floating-point value is rounded to an integer value V using the round toward zero rounding policy

/以上

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0