Scala 中将 Float 转成 Long 类型有三种方法,即:使用 asInstanceOf 方法、使用 toLong 方法以及使用 longValue() 函数。
var ret = FloatVarName.asInstanceOf[Long]
参数
参数 描述 FloatVarName 待转换的变量。 ret 转换后的结果。 说明
使用 asInstanceOf 函数传入 Long,可以将 Float 类型变量转成 Long 类型。
Scala toLong详解
语法
var ret = FloatVarName.toLong
参数
参数 描述 FloatVarName 待转换的变量。 ret 转换后的结果。 说明
使用 toLong 将 Float 类型变量转成 Long 类型。
Scala longValue()详解
语法
var ret = FloatVarName.longValue()
参数
参数 描述 FloatVarName 待转换的变量。 ret 转换后的结果。 说明
使用 longValue() 将 Float 类型变量转成 Long 类型。
案例
asInstanceOf将Float转Long
使用 asInstanceOf 实现将 Float 转 Long
object HaiCoder { def main(args: Array[String]) : Unit = { println("嗨客网(www.haicoder.net)!\n") val Age : Float = 109 var res = Age.asInstanceOf[Long] println("res =", res, "Type =", res.getClass) } }
程序运行,控制台输出,如下图:
我们使用了 Float 类型的变量调用 asInstanceOf[Long] 方法实现了将 Float 转成了 Long 类型。
toLong将Float转Long
使用 toLong 实现将 Float 转 Long
object HaiCoder { def main(args: Array[String]) : Unit = { println("嗨客网(www.haicoder.net)!\n") val Score : Float = 99 var res = Score.toLong println("Score =", res, "Type =", res.getClass) } }
程序运行,控制台输出,如下图:
我们使用了 Float 类型的变量调用 toLong 方法实现了将 Float 转成了 Long 类型。
longValue()将Float转Long
使用 longValue() 实现将 Float 转 Long
object HaiCoder { def main(args: Array[String]) : Unit = { println("嗨客网(www.haicoder.net)!\n") val A : Float = 1 var res = A.longValue() println("a =", res, "Type =", res.getClass) } }
程序运行,控制台输出,如下图:
我们使用了 Float 类型的变量调用 longValue() 方法实现了将 Float 转成了 Long 类型。
Scala Float转Long总结
Scala 中将 Float 转成 Long 类型有三种方法,即:使用 asInstanceOf 方法、使用 toLong 方法以及使用 longValue() 函数。