list_to_float

Håkan Stenholm <>
Wed Aug 9 19:56:16 CEST 2006


Demius Academius wrote:

> list_to_float("0") exited with badarg
> it must work as list_to_float("0.0")
>
you'll have to do something like:

str_to_float(Str) ->
    try float(list_to_integer(Str))
    catch error:_ -> list_to_float(Str)
    end.

or

str_to_float(Str) ->
    case string:to_float(Str) of
       {error, no_float} ->
             {Int, []} = case string:to_integer(Str),
             float(Int);
       {Float, []} -> Float
    end.

as "0" is considered a integer(), while "0.0" is treated as a float(). 
But I agree that it would be much nicer if list_to_float/1 and 
string:to_float/1 would do this by themselves.



More information about the erlang-bugs mailing list