[erlang-questions] list_to_float/1 precision

Per Hedeland per@REDACTED
Sat Dec 16 14:18:06 CET 2006


Hoan Ton-That <hoan@REDACTED> wrote:
>
>Is there a way to convert from strings to
>floats that is more precise?
>
>> list_to_float("-35.244818").
>-35.2448

The conversion is as precise as it can be (given 64-bit floats), what
you're seeing is just the default print format of the shell, which AFAIK
is 'p', which for floats is equivalent to 'g', which defaults to 6
significant digits (see the io(3) man page).

1> F=list_to_float("-35.244818").
-35.2448
2> io:format("~p~n",[F]).   
-35.2448
ok
3> io:format("~g~n",[F]).
-35.2448
ok
4> io:format("~.10g~n",[F]).
-35.24481800
ok

--Per Hedeland



More information about the erlang-questions mailing list