- Double Data type Usage and Type Conversion in Kotlin
- 1. Mentioning type explicitly
- 2. Number type is automatically inferred
- 3. Declaring and Initializing separately
- Double Datatype Example Program in Kotlin
- Sample Output
- Double type Conversion Example Program in Kotlin
- Sample Output
- Kotlin Data Types
- Read More Articles
- Different ways to convert a string to number in Kotlin
- Different ways to convert a string to number in Kotlin
Double Data type Usage and Type Conversion in Kotlin
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) < //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
- Read Data Input using Scanner in Kotlin
- Declare Variables 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
- Printing Variables and Values in Kotlin
- Float Data type Usage and Type Conversion in Kotlin
- Equality Operators (==, !=) and Referential equality Operators (===, !==) in Kotlin
- Long Data type Usage and Type Conversion in Kotlin
- Comparison Operators in Kotlin
- Byte Data type Usage and Type Conversion in Kotlin
- In Operator (in and !in) in Kotlin
- Is Operator (is and !is) in Kotlin
- Assignment Operators and Augmented Assignment Operators in Kotlin
- Read Data Input from Command Line in Kotlin
- Read String Data Input in Kotlin
- Char Data type Usage and Type Conversion in Kotlin
- Indexed Access Operator [, ] in Kotlin
- Elvis Operator (?:) in Kotlin
- Not Null Assertion(!!) Operator in Kotlin
- Logical Operators in Kotlin
- Safe Call Operator (?.) in Kotlin
- Repeat and Its Usage in Kotlin
- Boolean Data type Usage and Type Conversion in Kotlin
Different ways to convert a string to number in Kotlin
In this post, I will show you different ways to convert a string to number in Kotlin. This problem has a lot of use cases like your application is getting string values from the server and you want to convert it to number safely before processing. Or you want to convert the user input value to number before sending to the server. Let’s check the programs :
Convert to integer using toIntOrNull :
Thanks to Kotlin, we have one new method that makes this task easier for us. This is called toIntOrNull and below are its definitions :
fun String.toIntOrNull(): Int?
fun String.toIntOrNull(radix: Int): Int?
The second one throws one IllegalArgumentException if the radix is not valid.
Let’s take a look at the below example :
fun main() val strArray = arrayOf("10", "10.4", "0", "0.0", "-0", "-0.0", "-3", "-298889", "hello", "1234567", "AEAAAA", "12.3f") strArray.forEach println("$it => $it.toIntOrNull()>") > >
Here, we are creating one array of strings, we are iterating through the string elements one by one and getting the integer value using toIntOrNull. It will print the below output :
10 => 10 10.4 => null 0 => 0 0.0 => null -0 => 0 -0.0 => null -3 => -3 -298889 => -298889 hello => null 1234567 => 1234567 AEAAAA => null 12.3f => null
This is the easiest and the safest method to convert one string to number in Kotlin.
toInt() method converts the string to an integer. But if the string is not a valid integer, it throws one NumberFormatException. So, if you want to use this method, always make sure to wrap it inside a try-catch block.
The radix version throws one IllegalArgumentException if it is not a valid radix.
fun main() val strArray = arrayOf("10", "10.4", "0", "0.0", "-0", "-0.0", "-3", "-298889", "hello", "1234567", "AEAAAA", "12.3f") strArray.forEach try println("$it => $it.toInt()>") > catch (e: NumberFormatException) println("$it => null") > > >
10 => 10 10.4 => null 0 => 0 0.0 => null -0 => 0 -0.0 => null -3 => -3 -298889 => -298889 hello => null 1234567 => 1234567 AEAAAA => null 12.3f => null
Convert string to long, float and double :
Similar to integer, we have other methods to convert a string to long, float and double safely :
toLong() toLongOrNull() toFloat() toFloatOrNull() toDouble() toDoubleOrNull()
Let’s write the above program using all these methods. I am using only OrNull versions below :
fun main() val strArray = arrayOf("10", "10.4", "0", "0.0", "-0", "-0.0", "-3", "-298889", "hello", "1234567", "AEAAAA", "12.3f") strArray.forEach println("$it => Int: $it.toIntOrNull()>, Long: $it.toLongOrNull()>, Float: $it.toFloatOrNull()>, Double: $it.toDoubleOrNull()>") > >
10 => Int: 10, Long: 10, Float: 10.0, Double: 10.0 10.4 => Int: null, Long: null, Float: 10.4, Double: 10.4 0 => Int: 0, Long: 0, Float: 0.0, Double: 0.0 0.0 => Int: null, Long: null, Float: 0.0, Double: 0.0 -0 => Int: 0, Long: 0, Float: -0.0, Double: -0.0 -0.0 => Int: null, Long: null, Float: -0.0, Double: -0.0 -3 => Int: -3, Long: -3, Float: -3.0, Double: -3.0 -298889 => Int: -298889, Long: -298889, Float: -298889.0, Double: -298889.0 hello => Int: null, Long: null, Float: null, Double: null 1234567 => Int: 1234567, Long: 1234567, Float: 1234567.0, Double: 1234567.0 AEAAAA => Int: null, Long: null, Float: null, Double: null 12.3f => Int: null, Long: null, Float: 12.3, Double: 12.3
fun main() val strArray = arrayOf("FFFF","AEFF") strArray.forEach println("$it => Int: $it.toIntOrNull(16)>, Long: $it.toLongOrNull(16)>") > >
=> Int: 65535, Long: 65535 AEFF => Int: 44799, Long: 44799
Different ways to convert a string to number in Kotlin
In this post, I will show you different ways to convert a string to number in Kotlin. This problem has a lot of use cases like your application is getting string values from the server and you want to convert it to number safely before processing. Or you want to convert the user input value to number before sending to the server. Let’s check the programs :
Convert to integer using toIntOrNull :
Thanks to Kotlin, we have one new method that makes this task easier for us. This is called toIntOrNull and below are its definitions :
fun String.toIntOrNull(): Int?
fun String.toIntOrNull(radix: Int): Int?
The second one throws one IllegalArgumentException if the radix is not valid.
Let’s take a look at the below example :
fun main() val strArray = arrayOf("10", "10.4", "0", "0.0", "-0", "-0.0", "-3", "-298889", "hello", "1234567", "AEAAAA", "12.3f") strArray.forEach println("$it => $it.toIntOrNull()>") > >
Here, we are creating one array of strings, we are iterating through the string elements one by one and getting the integer value using toIntOrNull. It will print the below output :
10 => 10 10.4 => null 0 => 0 0.0 => null -0 => 0 -0.0 => null -3 => -3 -298889 => -298889 hello => null 1234567 => 1234567 AEAAAA => null 12.3f => null
This is the easiest and the safest method to convert one string to number in Kotlin.
toInt() method converts the string to an integer. But if the string is not a valid integer, it throws one NumberFormatException. So, if you want to use this method, always make sure to wrap it inside a try-catch block.
The radix version throws one IllegalArgumentException if it is not a valid radix.
fun main() val strArray = arrayOf("10", "10.4", "0", "0.0", "-0", "-0.0", "-3", "-298889", "hello", "1234567", "AEAAAA", "12.3f") strArray.forEach try println("$it => $it.toInt()>") > catch (e: NumberFormatException) println("$it => null") > > >
10 => 10 10.4 => null 0 => 0 0.0 => null -0 => 0 -0.0 => null -3 => -3 -298889 => -298889 hello => null 1234567 => 1234567 AEAAAA => null 12.3f => null
Convert string to long, float and double :
Similar to integer, we have other methods to convert a string to long, float and double safely :
toLong() toLongOrNull() toFloat() toFloatOrNull() toDouble() toDoubleOrNull()
Let’s write the above program using all these methods. I am using only OrNull versions below :
fun main() val strArray = arrayOf("10", "10.4", "0", "0.0", "-0", "-0.0", "-3", "-298889", "hello", "1234567", "AEAAAA", "12.3f") strArray.forEach println("$it => Int: $it.toIntOrNull()>, Long: $it.toLongOrNull()>, Float: $it.toFloatOrNull()>, Double: $it.toDoubleOrNull()>") > >
10 => Int: 10, Long: 10, Float: 10.0, Double: 10.0 10.4 => Int: null, Long: null, Float: 10.4, Double: 10.4 0 => Int: 0, Long: 0, Float: 0.0, Double: 0.0 0.0 => Int: null, Long: null, Float: 0.0, Double: 0.0 -0 => Int: 0, Long: 0, Float: -0.0, Double: -0.0 -0.0 => Int: null, Long: null, Float: -0.0, Double: -0.0 -3 => Int: -3, Long: -3, Float: -3.0, Double: -3.0 -298889 => Int: -298889, Long: -298889, Float: -298889.0, Double: -298889.0 hello => Int: null, Long: null, Float: null, Double: null 1234567 => Int: 1234567, Long: 1234567, Float: 1234567.0, Double: 1234567.0 AEAAAA => Int: null, Long: null, Float: null, Double: null 12.3f => Int: null, Long: null, Float: 12.3, Double: 12.3
fun main() val strArray = arrayOf("FFFF","AEFF") strArray.forEach println("$it => Int: $it.toIntOrNull(16)>, Long: $it.toLongOrNull(16)>") > >
=> Int: 65535, Long: 65535 AEFF => Int: 44799, Long: 44799