Double Data type Usage and Type Conversion in Kotlin
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
Kotlin Data Types
- Int Data type Usage and Type Conversion in Kotlin
- Float Data type Usage and Type Conversion in Kotlin
- Double Data type Usage and Type Conversion in Kotlin
- Byte Data type Usage and Type Conversion in Kotlin
- Short Data type Usage and Type Conversion in Kotlin
- Long Data type Usage and Type Conversion in Kotlin
- Char Data type Usage and Type Conversion in Kotlin
- Boolean Data type Usage and Type Conversion in Kotlin
- Number Data type Usage and Type Conversion in Kotlin
- String Data type Usage and Type Conversion in Kotlin
- Elvis Operator in Kotlin
Read More Articles
- Declare Variables In Kotlin
- Read Data Input using Scanner in Kotlin
- Double Data type Usage and Type Conversion in Kotlin
- print and println Data Output in Kotlin
- Arithmetic Operators (Mathematical Operators) in Kotlin
- Unary Operators (Sign, Inverts, Increment, and Decrement) in Kotlin
- Equality Operators (==, !=) and Referential equality Operators (===, !==) in Kotlin
- Printing Variables and Values in Kotlin
- Float Data type Usage and Type Conversion in Kotlin
- Long Data type Usage and Type Conversion in Kotlin
- Comparison Operators in Kotlin
- Byte Data type Usage and Type Conversion in Kotlin
- Is Operator (is and !is) in Kotlin
- In Operator (in and !in) in Kotlin
- Char Data type Usage and Type Conversion in Kotlin
- Read Data Input from Command Line in Kotlin
- Assignment Operators and Augmented Assignment Operators in Kotlin
- Indexed Access Operator [, ] in Kotlin
- Read String Data Input in Kotlin
- Short Data type Usage and Type Conversion in Kotlin
- Boolean Data type Usage and Type Conversion in Kotlin
- Logical Operators in Kotlin
- Elvis Operator (?:) in Kotlin
- Repeat and Its Usage in Kotlin
- Safe Call Operator (?.) in Kotlin