scala转int_如何在Scala中将Int转换为Double?

scala转int

Scala整数类型 (Scala Int Type)

Int data type in Scala is a numeric data type that stores integer values i.e. values without decimals. They are created using the int keyword.

Scala中的Int数据类型是一种数字数据类型,用于存储整数值,即不带小数的值。 它们是使用int关键字创建的。

It is can store a 32-bit signed value. The range is from -2147483648 to 2147483647.

它可以存储32位带符号的值。 范围是-2147483648至2147483647。

Syntax for creating integer in Scala,

在Scala中创建整数的语法,

    int integerValue = 3413;

Scala双型 (Scala Double Type)

Double data type in Scala is a numeric data type that stores floating-point values i.e. values with decimals. The double keyword is used to create a double variable in Scala. It can store a 64-bit IEEE double-precision float value.

Scala中的Double数据类型是一种数字数据类型,用于存储浮点值,即带小数的值。 double关键字用于在Scala中创建一个double变量。 它可以存储64位IEEE双精度浮点值。

Syntax for creating double in Scala,

在Scala中创建double的语法,

    double doubleValue = 112233445566;

在Scala中将Int转换为Double (Converting Int to Double in Scala)

The method to convert the data type of a variable in Scala is type Casting. The conversion of Int to Double can be done by using two methods,

在Scala中转换变量数据类型的方法是类型转换。 可以使用两种方法将Int转换为Double:

  1. toDouble method

    toDouble方法

  2. asInstanceOf[] method

    asInstanceOf []方法

Method 1: Converting Int to Double in Scala using toDouble method

方法1:使用toDouble方法在Scala中将Int转换为Double

The toDouble method in Scala is used to convert data type form any numeric type to Double in Scala.

Scala中的toDouble方法用于将数据类型从任何数字类型转换为Scala中的Double。

Syntax:

句法:

    var doubleVar : Double = intVal.toLong

Program to convert Int to Double

将Int转换为Double的程序

object MyObject {
    def main(args: Array[String]) {
        println("Int to Double conversion")
        
        // Creating an integer value 
        var intVar : Int = 10021998
        println("The integer value is : " + intVar)
        
        // Creating a double value
        var doubleVar : Double = 0
        
        // Converting the integer value to double value
        doubleVar = intVar.toDouble
        println("The double value is : " + doubleVar)
    }
}

Output:

输出:

Int to Double conversion
The integer value is : 10021998
The double value is : 1.0021998E7

Explanation:

说明:

In the above code, we have created a variable named intVar of Int type to store integer value. Then we have converted it to a Double value using the toDouble method and store it into variable doubleVar. Then at last we have printed the value using the println method.

在上面的代码中,我们创建了一个Int类型的名为intVar的变量来存储整数值。 然后,我们使用toDouble方法将其转换为Double值,并将其存储到变量doubleVar中 。 然后,最后我们使用println方法打印了该值。

Method 2: Converting Int to Double in Scala using asInstanceOf method

方法2:使用asInstanceOf方法在Scala中将Int转换为Double

The asInstanceOf[] method is used for converting between two data types. You can convert any data type to others using the asInstanceOf[] method.

asInstanceOf []方法用于在两种数据类型之间进行转换。 您可以使用asInstanceOf []方法将任何数据类型转换为其他数据类型。

This method is derived from java.lang package, though it does not an import statement, but it will work on for java data types i.e. not all Scala types can be converted using this method.

此方法派生自java.lang包,尽管它不是import语句,但适用于Java数据类型,即,并非所有Scala类型都可以使用此方法进行转换。

Syntax:

句法:

    var doubleVar = intVal.asInstanceOf[Double]

Program to convert int to double in Scala

在Scala中将int转换为double的程序

object MyObject {
    def main(args: Array[String]) {
        println("Int to Double conversion")
        
        // Creating an integer value 
        var intVar : Int = 431
        println("The integer value is : " + intVar)
        
        // Creating a double value
        var doubleVar : Double = 0
        
        // Converting the integer value to double value
        doubleVar = intVar.asInstanceOf[Double]
        println("The double value is : " + doubleVar)
    }
}

Output:

输出:

Int to Double conversion
The integer value is : 431
The double value is : 431.0

Explanation:

说明:

In the above code, we have created a variable named intVar of Int type to store an integer value. Then we have converted it to a Double value using the asInstanceOf[Double] method and store it into variable doubleVar. Then at last we have printed the value using the println method.

在上面的代码中,我们创建了一个Int类型的名为intVar的变量来存储整数值。 然后,我们使用asInstanceOf [Double]方法将其转换为Double值,并将其存储到变量doubleVar中 。 然后,最后我们使用println方法打印了该值。

翻译自: https://www.includehelp.com/scala/convert-int-to-double.aspx

scala转int