<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;">Thanks for the quick reply, Robbi. I was hoping to avoid the try-and-try-again approach b/c it doesn't extend so nicely to scanning multiple items from a string. Does Erlang provide any way to read what many other programming languages consider to be the printed representation of a number?<br><br>--- On <b>Mon, 4/20/09, Robert Raschke <i><rtrlists@googlemail.com></i></b> wrote:<br><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;">From: Robert Raschke <rtrlists@googlemail.com><br>Subject: Re: [erlang-questions] fread() for numbers - beginner question<br>To: branchingfactor@yahoo.com<br>Cc: erlang-questions@erlang.org<br>Date: Monday, April 20, 2009, 6:57 AM<br><br><pre>On Mon, Apr 20, 2009 at 10:43 AM, BranchingFactor<br><branchingfactor@yahoo.com>
wrote:<br>> I'd like to io_lib:fread() a number from a string. Unfortunately, if<br>I use<br>> "~f" for floating point number, then fread rejects numbers that<br>don't have<br>> fractional parts:<br>><br>>> io_lib:fread("~f", "1").<br>> {error,{fread,float}}<br>>> io_lib:fread("~f", "1.").<br>> {error,{fread,float}}<br>><br>> (I find this very strange, since both 1 and 1. can be represented in a<br>> floating point number.)<br>> And if I use "~d" for decimal integers, then fread won't<br>read past the<br>> decimal point:<br>><br>>> io_lib:fread("~d", "1.0").<br>> {ok,[1],".0"}<br>><br>> What is the easiest way to scan a number from a string in erlang? <br>Ideally<br>> it would scan any number in any input format and return an Erlang object<br>> that satisfied the is_number() predicate.<br><br>scan_number(S) -><br> case io_lib:fread("~f", S) of<br> {error, _}
-> io_lib:fread("~d", S);<br> Result -> Result<br> end.<br><br>This doesn't handle 16#ffff and friends though. but you can extend it<br>according to you needs.<br><br>And "1." is not a valid Erlang number.<br><br>Robby<br></pre></blockquote></td></tr></table><br>