Scala 将列表[双精度]转换为列表[字符串]
我通过调用一个函数得到一个双精度类型的列表,我需要将所有元素的这个列表[Double]转换为列表[String]。我该怎么做?我尝试使用toString(),但失败了。 我的名单是双重的:Scala 将列表[双精度]转换为列表[字符串],scala,Scala,我通过调用一个函数得到一个双精度类型的列表,我需要将所有元素的这个列表[Double]转换为列表[String]。我该怎么做?我尝试使用toString(),但失败了。 我的名单是双重的: List(153.0, 195.67, 212.33) 当我尝试将它转换为使用toString()时,它会给我一个错误 type mismatch; found : List[Double] required: List[String] 我的代码如下所示: val apiRes = ApiRespons
List(153.0, 195.67, 212.33)
当我尝试将它转换为使用toString()时,它会给我一个错误
type mismatch; found : List[Double] required: List[String]
我的代码如下所示:
val apiRes = ApiResponse.fromAPI(search) //returns List of Double
NewFormat.getVal(x.id, search, apiPrices.toString) // Type Mismatch
尽管在REPL上,我觉得不错。您需要使用
List.map
并调用toString
:
NewFormat.getVal(x.id, search, apiPrices.map(_.toString))
在列表[Double]
上调用toString
将生成一个字符串