Bhaskar Mishra Bhaskar Mishra - 4 months ago 12
Scala Question

Convert List[Double] to List[String]

I get a List of Double type from calling a function, I need to convert this List[Double] to List[String] for all elements. How can i do this? I tried using toString() but it fails.
My List Double :

List(153.0, 195.67, 212.33)


and when I try doing to convert it into using toString() it gives me an error as

type mismatch; found : List[Double] required: List[String]


my code looks like :

val apiRes = ApiResponse.fromAPI(search) //returns List of Double
NewFormat.getVal(x.id, search, apiPrices.toString) // Type Mismatch


though on REPL it looks good to me.

Answer

You need to use List.map and call toString:

NewFormat.getVal(x.id, search,  apiPrices.map(_.toString))

Calling toString on a List[Double] will yield a String.