This should be a silly question.
scala> val aFloat = 1.5f
aFloat: Float = 1.5
How to cast aFloat to an Int in a simple way?
I already know to use a.asInstanceOf[Int]
. But it needs too much keystrokes.
This should be a silly question.
scala> val aFloat = 1.5f
aFloat: Float = 1.5
How to cast aFloat to an Int in a simple way?
I already know to use a.asInstanceOf[Int]
. But it needs too much keystrokes.
1.5f.toInt
//--> res0: Int = 1
You have toDouble, toFloat, toInt and toLong on all number types.
as well as the toFloat, toInt, etc. methods, you can also use type ascription in some cases:
val b = 23 : Byte