开发者

How to do simple type cast in Scala?

This should be a silly question.

scala> val aFloat = 1.5f
aFloat: Float = 1.5

How 开发者_如何学JAVAto 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜