LoginSignup
9
5

More than 5 years have passed since last update.

Scala: Int同士の割り算は、Intが戻り値なので注意

Last updated at Posted at 2015-03-02

整数同士の割り算、例えば 100000 ÷ 30 = 3333.333333333 の結果はFloatになりそうですが、Scalaは3333のようにIntが戻り値になるので注意が必要です。

計算結果がInt
object Main extends App {
  val sales = 100000
  val days = 30
  val salesPerDay = sales / days
  println(salesPerDay) // 3333
}

計算結果を少数として欲しい時は、計算に使う数字をFloatに変換しておく必要があります。

計算結果がFloat
object Main extends App {
  val sales = 100000
  val days = 30
  val salesPerDay = sales / days.toFloat
  println(salesPerDay) // 3333.3333
}
9
5
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
9
5