Scala Long转Double

Scala Long转Double教程

Scala 中将 Long 转成 Double 类型有三种方法,即:使用 asInstanceOf 方法、使用 toDouble 方法以及使用 doubleValue() 函数。

Scala Long转Double详解

语法

var ret = LongVarName.asInstanceOf[Double]

参数

参数 描述
LongVarName 待转换的变量。
ret 转换后的结果。

说明

使用 asInstanceOf 函数传入 Double,可以将 Long 类型变量转成 Double 类型。

Scala toDouble详解

语法

var ret = LongVarName.toDouble

参数

参数 描述
LongVarName 待转换的变量。
ret 转换后的结果。

说明

使用 toDouble 将 Long 类型变量转成 Double 类型。

Scala doubleValue()详解

语法

var ret = LongVarName.doubleValue()

参数

参数 描述
LongVarName 待转换的变量。
ret 转换后的结果。

说明

使用 doubleValue() 将 Long 类型变量转成 Double 类型。

案例

asInstanceOf将Long转Double

使用 asInstanceOf 实现将 Long 转 Double

object HaiCoder { def main(args: Array[String]) : Unit = { println("嗨客网(www.haicoder.net)!\n") val Num : Long = 109 var res = Num.asInstanceOf[Double] println("Num =", res, "Type =", res.getClass) } }

程序运行,控制台输出,如下图:

51_Scala Long转Double.png

我们使用了 Long 类型的变量调用 asInstanceOf[Double] 方法实现了将 Long 转成了 Double 类型。

toDouble将Long转Double

使用 toDouble 实现将 Long 转 Double

object HaiCoder { def main(args: Array[String]) : Unit = { println("嗨客网(www.haicoder.net)!\n") val Score : Long = 99 var res = Score.toDouble println("Score =", res, "Type =", res.getClass) } }

程序运行,控制台输出,如下图:

52_Scala Long转Double.png

我们使用了 Long 类型的变量调用 toDouble 方法实现了将 Long 转成了 Double 类型。

doubleValue()将Long转Double

使用 doubleValue() 实现将 Long 转 Double

object HaiCoder { def main(args: Array[String]) : Unit = { println("嗨客网(www.haicoder.net)!\n") val A : Long = 1 var res = A.doubleValue() println("a =", res, "Type =", res.getClass) } }

程序运行,控制台输出,如下图:

53_Scala Long转Double.png

我们使用了 Long 类型的变量调用 doubleValue() 方法实现了将 Long 转成了 Double 类型。

Scala Long转Double总结

Scala 中将 Long 转成 Double 类型有三种方法,即:使用 asInstanceOf 方法、使用 toDouble 方法以及使用 doubleValue() 函数。