Newsgroups: comp.lang.lisp
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!oitnews.harvard.edu!purdue!lerc.nasa.gov!magnus.acs.ohio-state.edu!math.ohio-state.edu!howland.reston.ans.net!newsfeed.internetmci.com!in1.uu.net!harlequin.com!epcot!usenet
From: norvig@meteor.harlequin.com (Peter Norvig)
Subject: Re: Convert Strings Into Numbers (integer, float)
In-Reply-To: misha@ccs.neu.edu's message of 11 Jan 1996 07:21:49 GMT
Message-ID: <NORVIG.96Jan11173302@meteor.harlequin.com>
Lines: 24
Sender: usenet@harlequin.com (Usenet Maintainer)
Nntp-Posting-Host: meteor.menlo.harlequin.com
Organization: Harlequin, Inc., Menlo Park, CA
References: <4d0gk7$t63@news.rz.uni-passau.de> <4d2dqd$c50@camelot.ccs.neu.edu>
Date: Fri, 12 Jan 1996 01:33:02 GMT


> Klaus Berndl (berndl@vogelweide.uni-passau.de) wrote:
> 
> : How to convert a suitable string (i.e. the string contents only digits and perhaps one dot) into a number-format like integer (only digits allowed) or float (only digits and one dot allowed)?


Here's a solution:

(defun parse-float (string)
  "Return a float read from string, and the index to the remainder of string."
  (multiple-value-bind (integer i)
      (parse-integer string :junk-allowed t)
    (multiple-value-bind (fraction j)
	(parse-integer string :start (+ i 1) :junk-allowed t)
      (values (float (+ integer (/ fraction (expt 10 (- j i 1))))) j))))

CL-USER> (parse-float "  123.456 ")
123.456
9
-- 
Peter Norvig                  | Phone: 415-833-4022           FAX: 415-833-4111
Harlequin Inc.                | Email: norvig@harlequin.com
1010 El Camino Real, #310     | http://www.harlequin.com
Menlo Park CA 94025           | http://www.cs.berkeley.edu/~russell/norvig.html
