[erlang-questions] string:to_float (was: string:to_lower)

Dustin Sallings dustin@REDACTED
Sun Sep 2 22:53:27 CEST 2007


On Sep 2, 2007, at 7:54, Zac Brown wrote:

> Anthony Shipman wrote:
>> 13> string:to_float("0").
>> {error,no_float}
>> 14> string:to_float("0.0").
>> {0.00000e+0,[]}
>>
>> That's a bit rough!
>>
> Hi Anthony:
>
>     From the erlang docs for to_float(String):
>                    *Argument |String| is expected to start with a  
> valid
> text represented float (the digits being ASCII values). Remaining
> characters in the string after the float are returned in |Rest|.
>
> *    It would seem that "0" is not recognized as a float and thats
> probably for some management issues in the VM :).

	I don't think there should be distinction here. It can definitely be  
made to work:

to_float(S) ->
     case string:to_float(S) of
         {error, no_float} ->
             list_to_integer(S) * 1.0;
         {X,_More} -> X
     end.


	or

to_float(S) ->
     case catch(list_to_float(S)) of
         {'EXIT', {badarg, _Stuff}} ->
             list_to_integer(S) * 1.0;
         X -> X
     end.



	IMO, it's a bit counterintuitive (especially for the users of my  
applications) that I can't read ``1'' as a float without doing weird  
stuff.

-- 
Dustin Sallings





More information about the erlang-questions mailing list