How to extract multiple floats from a string of floats in Kotlin

1 Answer

0 votes
fun main() {
    val str = "2.809 -36.91 21.487 -493.808 5034.7001"

    val tokens = str.split(" ")

    val floats = tokens.map { it.toFloat() }

    floats.forEach { println(it) }
}

 
  
/*
run:
 
2.809
-36.91
21.487
-493.808
5034.7

*/

 



answered Jul 28, 2025 by avibootz
...