开发者

How to do simple type cast in Scala?

开发者 https://www.devze.com 2023-01-18 04:55 出处:网络
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?

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

精彩评论

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