codedancer
codedancer

Reputation: 1634

How to convert Vector{Decimal} to Float64 in Julia?

I am just starting Julia and doing some column manipulation. However, I found myself into a column type called Vector{Decimal}. I'd like to convert it into something more common like Float64.

I tried to use the convert(Float64, df.difference), but I got this foloowing error:

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

What's the best way to handle this type of column? Here is my dataframe:

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

Upvotes: 4

Views: 376

Answers (1)

Przemyslaw Szufel
Przemyslaw Szufel

Reputation: 42194

Just do:

Float64.(df.difference)

For an example:

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

Upvotes: 4

Related Questions