如何使用Scala将arraybuffer中的字符串解析为double?

时间:2018-08-23 15:05:45

标签: scala playframework play-json

我正在尝试将字符串映射到通过Playframework解析的ArrayBuffer的两倍,但我不断遇到以下错误:

<Button x:Name="Accept_Button" Content="Accept" HorizontalAlignment="Left" Margin="475,316,0,0" VerticalAlignment="Top" Width="81" 
        Command="{Binding DataContext.Window1Command, RelativeSource={RelativeSource AncestorType=Window}}"/>

我不确定为什么要这样做,并且我是来自Python背景的Scala的新手。

Exception in thread "main" java.lang.NumberFormatException: For input string: ""0.04245800""

1 个答案:

答案 0 :(得分:2)

如果您确定您的列表中仅包含字符串,则可以将其像这样进行转换,并使用“原始”值从以下项中获取Double值:

println(prices.map(_.as[JsString].value.toDouble))

由于JsString不是字符串,因此无法在其上调用toDouble。

仅出于完整性考虑:如果不确定列表是否仅包含字符串,则应添加checkof或pattern匹配的实例。

相关问题