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