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