Convert string to double 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

  1. Int Data type Usage and Type Conversion in Kotlin
  2. Float Data type Usage and Type Conversion in Kotlin
  3. Double Data type Usage and Type Conversion in Kotlin
  4. Byte Data type Usage and Type Conversion in Kotlin
  5. Short Data type Usage and Type Conversion in Kotlin
  6. Long Data type Usage and Type Conversion in Kotlin
  7. Char Data type Usage and Type Conversion in Kotlin
  8. Boolean Data type Usage and Type Conversion in Kotlin
  9. Number Data type Usage and Type Conversion in Kotlin
  10. String Data type Usage and Type Conversion in Kotlin
  11. Elvis Operator in Kotlin
Читайте также:  Python current timestamp in milliseconds
  1. Read Data Input using Scanner in Kotlin
  2. Declare Variables In Kotlin
  3. Double Data type Usage and Type Conversion in Kotlin
  4. print and println Data Output in Kotlin
  5. Arithmetic Operators (Mathematical Operators) in Kotlin
  6. Unary Operators (Sign, Inverts, Increment, and Decrement) in Kotlin
  7. Printing Variables and Values in Kotlin
  8. Float Data type Usage and Type Conversion in Kotlin
  9. Equality Operators (==, !=) and Referential equality Operators (===, !==) in Kotlin
  10. Long Data type Usage and Type Conversion in Kotlin
  11. Comparison Operators in Kotlin
  12. Byte Data type Usage and Type Conversion in Kotlin
  13. In Operator (in and !in) in Kotlin
  14. Is Operator (is and !is) in Kotlin
  15. Assignment Operators and Augmented Assignment Operators in Kotlin
  16. Read Data Input from Command Line in Kotlin
  17. Read String Data Input in Kotlin
  18. Char Data type Usage and Type Conversion in Kotlin
  19. Indexed Access Operator [, ] in Kotlin
  20. Elvis Operator (?:) in Kotlin
  21. Not Null Assertion(!!) Operator in Kotlin
  22. Logical Operators in Kotlin
  23. Safe Call Operator (?.) in Kotlin
  24. Repeat and Its Usage in Kotlin
  25. 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 :

Читайте также:  Square root in html

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 

Источник

Оцените статью