简体   繁体   English

将List [Double]转换为List [String]

[英]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. 我从调用函数得到一个Double类型的List,我需要将所有元素的List [Double]转换为List [String]。 How can i do this? 我怎样才能做到这一点? I tried using toString() but it fails. 我尝试使用toString()但它失败了。 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 当我尝试将其转换为使用toString()时,它给出了一个错误

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. 虽然在REPL上它对我来说很好看。

You need to use List.map and call toString : 您需要使用List.map并调用toString

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

Calling toString on a List[Double] will yield a String . List[Double]上调用toString将产生一个String

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM