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

1 Answer

0 votes
object ExtractFloatsFromString_Scala {
  def main(args: Array[String]): Unit = {
    val str = "2.809 -36.91 21.487 -493.808 5034.7001"

    val tokens = str.split(" ")

    val floats = tokens.map(_.toFloat)

    floats.foreach(println)
  }
}



/*
run:
 
2.809
-36.91
21.487
-493.808
5034.7
 
*/
 

 



answered Jul 28, 2025 by avibootz
edited Jul 28, 2025 by avibootz
...