[erlang-questions] fread() for numbers - beginner question
Joe Armstrong
erlang@REDACTED
Tue Apr 21 16:37:49 CEST 2009
How about using the tokeniser that Erlang uses?
> erl_scan:string("pi,is,3.14159,\"strings\",1e10,1234").
{ok,[{atom,1,pi},
{',',1},
{atom,1,is},
{',',1},
{float,1,3.14159},
{',',1},
{string,1,"strings"},
{',',1},
{integer,1,1},
{atom,1,e10},
{',',1},
{integer,1,1234}],
1}
> {ok, [{float,_,F}], _} = erl_scan:string("3.14159").
{ok,[{float,1,3.14159}],1}
> F.
3.14159
etc.
/Joe
On Mon, Apr 20, 2009 at 11:43 AM, BranchingFactor
<branchingfactor@REDACTED> wrote:
> I'd like to io_lib:fread() a number from a string. Unfortunately, if I use
> "~f" for floating point number, then fread rejects numbers that don't have
> fractional parts:
>
>> io_lib:fread("~f", "1").
> {error,{fread,float}}
>> io_lib:fread("~f", "1.").
> {error,{fread,float}}
>
> (I find this very strange, since both 1 and 1. can be represented in a
> floating point number.)
> And if I use "~d" for decimal integers, then fread won't read past the
> decimal point:
>
>> io_lib:fread("~d", "1.0").
> {ok,[1],".0"}
>
> What is the easiest way to scan a number from a string in erlang? Ideally
> it would scan any number in any input format and return an Erlang object
> that satisfied the is_number() predicate.
>
> Thank you,
>
> BF
>
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
More information about the erlang-questions
mailing list