list_to_float

James Hague jamesh@REDACTED
Wed Aug 9 20:06:49 CEST 2006


Actually list_to_number would be a good standard library addition.  It would
call list_to_integer first, then if that failed, call list_to_float.

-----Original Message-----
From: owner-erlang-bugs@REDACTED [mailto:owner-erlang-bugs@REDACTED] On
Behalf Of Håkan Stenholm
Sent: Wednesday, August 09, 2006 12:56 PM
To: Demius Academius
Cc: erlang-bugs@REDACTED
Subject: Re: list_to_float

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