如何在Julia中将Vector{Decimal}转换为Float64



我刚刚启动Julia并进行一些列操作。然而,我发现自己进入了一个名为Vector{Decimal}的列类型。我想把它转换成更常见的东西,比如Float64。

我试着使用convert(Float64, df.difference),但我得到了以下错误:

ERROR: MethodError: Cannot `convert` an object of type Vector{Decimals.Decimal} to an object of type Float64

处理这种类型的列的最佳方法是什么?这是我的数据帧:

julia> df1[1:5, :]
5×3 DataFrame
Row │ base_msrp  sales_amount  difference
│ Decimal…?  Float64?      Decimal…
─────┼─────────────────────────────────────
1 │    599.99       479.992     119.998
2 │    599.99       599.99       -0.00
3 │    599.99       479.992     119.998
4 │    599.99       539.991      59.999
5 │    599.99       539.991      59.999

只需执行:

Float64.(df.difference)

例如:

julia> using Decimals
julia> vals = Decimal.(rand(3))
3-element Vector{Decimal}:
Decimal(0, 8265655272182808, -16)
Decimal(0, 8864515364687842, -16)
Decimal(0, 7020504368500311, -16)
julia> Float64.(vals)
3-element Vector{Float64}:
0.8265655272182808
0.8864515364687842
0.7020504368500311

最新更新