tags:

views:

143

answers:

2

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.

+12  A: 
1.5f.toInt

//--> res0: Int = 1

You have toDouble, toFloat, toInt and toLong on all number types.

Landei
+3  A: 

as well as the toFloat, toInt, etc. methods, you can also use type ascription in some cases:

val b = 23 : Byte
Kevin Wright