Double Data type Usage and Type Conversion in Kotlin
On this page (11sections)
Definition
- Double is a 64-bit number in Kotlin. Any decimal number without mentioning the number type is a double number.
- Double number is tagged with “d” or “D”
- The type can be either declared explicitly or the compiler itself has the ability to infer the type of the assigned value.
- In JVM, the characteristics of this “Double” variable is derived from the characteristics of primitive type double of Java which has a non-nullable default value.
Syntax and declaration
Thus a Double value can be declared by 3 ways as mentioned below.
1. Mentioning type explicitly
val variable_name : Double = value
for example,
val number : Double = 100.50
2. Number type is automatically inferred
val variable_name = value
for example,
//Number type is automatically inferred
val number = 100.50
3. Declaring and Initializing separately
var variable_name : Double
variable_name = value
for example
//Declaring and Initializing separately
var number:Double
number = 100.50
Double Datatype Example Program in Kotlin
// Double Datatype Kotlin example program
// Data type Kotlin Programs, Basic Kotlin Programs
fun main(args:Array<String>) {
//Assigning value to Double variable explicitly
val a : Double = 22.8e5
//Assigned value is inferred automatically by compiler
val b = 100.0
println("Value of a : $a")
println("Value of b : $b")
}
Sample Output
Value of a : 2280000.0
Value of b : 100.0
Double type Conversion Example Program in Kotlin
// Double Datatype Kotlin example program
// Data type Kotlin Programs, Basic Kotlin Programs
fun main(args: Array < String > ) {
//Converted from String and Double type is inferred
val num1 = "500".toDouble()
//Conversion from String and Declaring an Double value immediately
val num2: Double = "100.01".toDouble()
val num3: Int = 1000
//Conversion from Int
val num4: Double = num3.toDouble()
val num5: Float = 2000.31f
//Conversion from Float
val num6: Double = num5.toDouble()
//Print values after conversion
println("String to Double : num1 Value : $num1")
println("String to Double : num2 Value : $num2")
println("Int Value : num3 Value : $num3")
println("Int to Double : num4 Value : $num4")
println("Float Value : num5 Value : $num5")
println("Float to Double : num6 Value : $num6")
}
Sample Output
String to Double : num1 Value : 500.0
String to Double : num2 Value : 100.01
Int Value : num3 Value : 1000
Int to Double : num4 Value : 1000.0
Float Value : num5 Value : 2000.31
Float to Double : num6 Value : 2000.31005859375
How It Works
This Kotlin program demonstrates Double Data type Usage and Type Conversion. It first prepares the data it needs, then performs the core operation step by step, and finally prints the output shown in the Sample Output above.
- Declare the variables that hold the program’s data.
- Print the final result to the console so you can compare it with the sample output.
Try changing the input values and re-running the program to see how the output changes — this is the fastest way to understand how the logic behaves.
Frequently Asked Questions
What is Double Data type Usage and Type Conversion in Kotlin?
Double is a 64-bit number in Kotlin.
How do I run this Kotlin example?
Run it in IntelliJ IDEA or Android Studio, or compile from the command line with `kotlinc file.kt -include-runtime -d file.jar` and run `java -jar file.jar`.
How can I practice Double Data type Usage and Type Conversion?
Copy the example into IntelliJ IDEA or Android Studio, run it, then change the values or add print statements to see how the output changes.