How to convert a decimal string to a double in Kotlin

2 Answers

0 votes
fun main() {
    val s = "12.34"
    val d = s.toDouble()

    println("String value: $s")
    println("Double value: $d")
}


/*
run:

String value: 12.34
Double value: 12.34

*/

 



answered Apr 12 by avibootz
0 votes
fun main() {
    val s = "12.34"
    val d = s.toDoubleOrNull() // safely option
    
    println("String value: $s")
    println("Double value: $d")
}


/*
run:

String value: 12.34
Double value: 12.34

*/

 



answered Apr 12 by avibootz
...