list_to_float

Raimo Niskanen <>
Thu Aug 10 11:10:48 CEST 2006


There have been external and internal discussions about this one.

Anyway, the semantics can not be changed now. There must be
lots of written code that relies on an exception from
erlang:list_to_float("0"), since it is not a valid 
erlang float - it is an integer.

An erlang:list_to_number/1 might be an useful addition, but
it is easy enough to write for yourself. If such a function
is desired, I can imagine an an erlang:list_to_term/1 also
(instead?) would be. It should do all the erl_scan:string(S), 
erl_parse:parse_term(T) that repeatedly pops up on
erlang-questions. Erlang shell example:
        1> f(S),S="{tuple}",f(T),{ok,T,_}=erl_scan:string(S++".").
        2> f(X),{ok,X}=erl_parse:parse_term(T),X.
        {tuple}
But that is a beast that involves erl_scan and erl_parse
functionality into the erlang module itself - mixing
run-time with compile-time. It might fit better into
some other module, the question remains which.



Enough rambling. Here is another fix for your problem:
        how_to_float(S) ->
            case string:to_float(S++".0") of
                {F,_} when is_float(F) -> F;
                _ -> erlang:error(badarg, [S]);
            end.
Assuming S is known to not be huge. You probably only
need to pick out the best parts of the case statement
(you get the idea, ``string:to_float(S++".0")'' is the trick).



 (Demius Academius) writes:

> list_to_float("0") exited with badarg
> it must work as list_to_float("0.0")

-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB



More information about the erlang-bugs mailing list