# How to convert Int to Double in Scala?

Scala | Converting Int to Double: Here, we are going to learn how to convert Int to Double in Scala?
Submitted by Shivang Yadav, on May 28, 2020

## 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.

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

Syntax for creating integer in Scala,

```    int integerValue = 3413;
```

## 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.

Syntax for creating double in Scala,

```    double doubleValue = 112233445566;
```

## 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,

1. toDouble method
2. asInstanceOf[] method

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

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

Syntax:

```    var doubleVar : Double = intVal.toLong
```

Program to convert Int to 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.

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

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

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.

Syntax:

```    var doubleVar = intVal.asInstanceOf[Double]
```

Program to convert int to double in Scala

```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.

Preparation

What's New

Top Interview Coding Problems/Challenges!