Parsing Float64 as Float64

Hi,

Reading data from various excel files, sometimes it is correctly formatted as a Float, sometimes it is interpreted as a string.

If I account for the second case, I must use parse(Float64, input). Using this code in the first case gives the error

no method matching parse(::Type{Float64}, ::Float64)

It seems odd that the parser cannot interpret a Float64 number as… a Float64 number.

The workaround is to tediously reproduce code for both instances, whereas accepting Float64 as an argument means the same code would work either way.

C

It looks like parse is only for converting strings into numbers, and doesn’t work on any other type (for that, you could use convert instead).

This is a little hacky, but you could try turning everything into a string first?

parse(Float64, string(x))
1 Like

That’s probably this issue:

https://github.com/felipenoris/XLSX.jl/issues/192

You can do x -> x isa Number ? x : parse(Float64, x)

1 Like

I think it’s good that parsing is restricted to strings, but you can make your own parsing function that is more lenient. This one always accepts a value of type T as is if we’re parsing that T. One could think of even more lenient variants:

sloppy_parse(::Type{T}, x::T) where T = x
sloppy_parse(T, x) = parse(T, x)