【发布时间】:2015-10-30 16:39:06
【问题描述】:
我正在尝试将从数据库中提取的数字数据写入Float64[]。原始数据为::ASCIIString 格式,因此尝试将其推送到数组会出现以下错误:
julia> push!(a, "1")
ERROR: MethodError: `convert` has no method matching convert(::Type{Float64}, ::ASCIIString)
This may have arisen from a call to the constructor Float64(...),
since type constructors fall back to convert methods.
Closest candidates are:
call{T}(::Type{T}, ::Any)
convert(::Type{Float64}, ::Int8)
convert(::Type{Float64}, ::Int16)
...
in push! at array.jl:432
尝试直接转换数据会引发同样的错误:
julia> convert(Float64, "1")
ERROR: MethodError: `convert` has no method matching convert(::Type{Float64}, ::ASCIIString)
This may have arisen from a call to the constructor Float64(...),
since type constructors fall back to convert methods.
Closest candidates are:
call{T}(::Type{T}, ::Any)
convert(::Type{Float64}, ::Int8)
convert(::Type{Float64}, ::Int16)
...
鉴于我知道数据是数字,有没有办法可以在推送之前对其进行转换?
附言我使用的是 0.4.0 版
【问题讨论】:
-
顺便说一句,考虑使用
tryparse(Float64,x)而不是parse。它返回一个 Nullable Float,在字符串解析不好的情况下为 null。 -
好建议,干杯。顺便说一句,如果你想写一个答案,我会接受它,否则我会在一两天内为了完整性而写一些东西。
标签: string floating-point type-conversion julia