Why can't Julia parse functions parse numbers in scientific notation with d instead of e?

115 views Asked by At

I am writing a program where I need to parse numbers written in scientific notation (as 0.00812216d0) from a file. It turns out that Julia parse(Type,str) can't parse these numbers because of 'd'.

It works well if I swap 'd' with 'e'. However, it's very unpractical to write replace function everywhere. I can't just do it for the whole text file, as it would cause some unwanted changes. Any solution?

1

There are 1 answers

0
Bill On

Try (if you want double precision, ie 64 bit floats):

dparse(s) = parse(Float64, replace(s, 'd' => 'e'))

as BallpontBen said in comments. For BigFloat, replace 'Float64' with BigFloat above. If you get errors from your data parsing, consider

dparse(s) = something(tryparse(Float64, replace(s, 'd' => 's')), missing)

which will give you missing for parse errors instead of throwing an error.