Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Convert Strings Into Numbers (integer, float)

1,789 views
Skip to first unread message

k p c

unread,
Jan 10, 1996, 3:00:00 AM1/10/96
to Klaus Berndl, k...@ptolemy.arc.nasa.gov
Quoth ber...@vogelweide.uni-passau.de (Klaus Berndl):
> Exists there a function or a macro in Common Lisp (Allegro CL 4.2)?

You can use read-from-string and parse-integer. read-from-string is
the comprehensive way to do it. Look at the documentation for
with-standard-io-syntax and *read-eval*.

Note that my profiling shows that Allegro CL 4.2 read-from-string is
slow.

Consider a freeware parse-float that is floating around somewhere. I
don't know if it is more efficient than read-from-string or not,
however.

By the way, please keep your line lengths below 77 characters.

If you post a followup to this article, I would appreciate a courtesy
verbatim copy by email to help work around potentially unreliable feeds.

---
k...@ptolemy.arc.nasa.gov. AI, multidisciplinary neuroethology, info filtering.

Klaus Berndl

unread,
Jan 10, 1996, 3:00:00 AM1/10/96
to
My problem:

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)?


Exists there a function or a macro in Common Lisp (Allegro CL 4.2)?

Ciao,
KLaus Berndl

e-mail: ber...@fmi.uni-passau.de

Michael Tselman

unread,
Jan 11, 1996, 3:00:00 AM1/11/96
to
Klaus Berndl (ber...@vogelweide.uni-passau.de) wrote:
: My problem:

: Ciao,
: KLaus Berndl

: e-mail: ber...@fmi.uni-passau.de

In a standard Common Lisp you can use the function

-----------------------------------------------------------------------------
PARSE-INTEGER [Function]

Function in LISP package:
Args: (string
&key (start 0) (end (length string)) (radix 10) (junk-allowed nil))
Parses STRING for an integer and returns it.

-----------------------------------------------------------------------------

Using it you can also easily write a parse-float function for the floats in
your format.


--Misha

--
-----------------------------------------------------------------------------+
Michael Tselman (Misha) Internet: mi...@ccs.neu.edu | Imagination is |
College of Computer Science, Northeastern University | more important |
23 Cullinane Hall, 360 Huntington Ave., Boston, MA 02115| than knowledge!|
Phone: (617)-373-3822, Fax: (617)-373-5121 | |
WWW: http://www.ccs.neu.edu/home/misha | (A. Einstein) |
-----------------------------------------------------------------------------+

Peter Norvig

unread,
Jan 12, 1996, 3:00:00 AM1/12/96
to

> Klaus Berndl (ber...@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: nor...@harlequin.com
1010 El Camino Real, #310 | http://www.harlequin.com
Menlo Park CA 94025 | http://www.cs.berkeley.edu/~russell/norvig.html

Antonio Leitao

unread,
Jan 13, 1996, 3:00:00 AM1/13/96
to Klaus Berndl
>>>>> "Klaus" == Klaus Berndl <ber...@vogelweide.uni-passau.de> writes:

Klaus> My problem:
Klaus> 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)?
Klaus> Exists there a function or a macro in Common Lisp (Allegro CL 4.2)?

'read-from-string' solves your problem

Hope this helps

Antonio Leitao

Antonio Leitao

unread,
Jan 13, 1996, 3:00:00 AM1/13/96
to Klaus Berndl

I forgot to add some documentation:

FUNCTION
read-from-string - read an object from a string

Package LISP

Usage
read-from-string string [eof-error-p [eof-value [:start start]
[:end end] [:preserve-whitespace preserve]]]

DESCRIPTION
Returns two values: the LISP object read from successive characters in
string and the index of the first character in string that was not read.

You can read from a substring of string by specifying values for the
keyword arguments :start and :end. The keyword argument :start speci-
fies the index of the first character in string to read. Its value
defaults to 0, denoting the beginning of string. The keyword argument
:end, specifies an index one greater than the index of the last charac-
ter to read. Its value may be an integer greater than or equal to the
value of the :start argument and less than or equal to the length of
string, or nil. The value nil is the same as the default value, the
length of string.

If the value of the keyword argument :preserve-whitespace is specified
non-nil, white space will be preserved in the same manner as
read-preserving-whitespace.

The argument eof-error-p controls what happens when the end of string is
reached. If the value of the argument is t, the default, an error is
signaled. However, if the value of the argument is nil, then in most
situations an error is not signaled. Instead, the read-from-string
function terminates and returns the value of eof-value. eof-value
defaults to nil. The function read-from-string always signals an error
if the end of string is reached when a COMMON LISP object is partially
but not completely read.

EXAMPLES
(read-from-string "this is a test") => this 5
(read-from-string "this is a test" nil nil :preserve-whitespace t)
=> this 4
(read-from-string "this is a test" nil 'done :start 5) => is 8
(read-from-string "this is a test" nil 'done :start 5 :end 6)
=> i 6
(read-from-string "this is a test" nil 'done :start 14) => done 14

Sorry about the two-part answer.
Good Luck.

Antonio Leitao

Antonio Leitao

unread,
Jan 15, 1996, 3:00:00 AM1/15/96
to Klaus Berndl
I am sorry because I have incorrectly posted some Franz, Inc
copyrighted documentation without permission.

I was trying to suggest the function read-from-string when I quoted
directly from Franz's CommonLisp documentation without even including
the copyright line.

Again, I am sorry. I never intended to get credit for the answer. I
just thought it would be helpful to include the function
documentation and completely forgot it was Franz's documentation and
not mine's.

Thanks for Erik Naggum for politely remind me of my fault (and suggest
a correction).

I will try to pay more attention in the future.

Antonio Leitao

Hillary Ryan

unread,
Jun 22, 2013, 10:56:49 PM6/22/13
to
When I tried this code, I noticed that it crashed when given integers like (parse-float "1"). I fixed it like this. I haven't thoroughly tested it, so please excuse any errors.

(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)
(if (= (length string) i)
(values integer i)
(multiple-value-bind (fraction j)
(parse-integer string :start (+ i 1) :junk-allowed t)
(if (null fraction)
(values integer i)
(values (float (+ integer (/ fraction (expt 10 (- j i 1))))) j))))))


Example:
CL-USER> (parse-float "1a b c")
1
1

Pascal J. Bourguignon

unread,
Jun 23, 2013, 9:52:05 AM6/23/13
to
See also the papers referenced here:

http://stackoverflow.com/questions/85223/how-to-manually-parse-a-floating-point-number-from-a-string

The "standard" algorithm for converting a decimal number to the best
floating-point approximation is William Clinger's How to read floating
point numbers accurately, downloadable from here. Note that doing this
correctly requires multiple-precision integers, at least a certain
percentage of the time, in order to handle corner cases.

Algorithms for going the other way, printing the best decimal number
from a floating-number, are found in Burger and Dybvig's Printing
Floating-Point Numbers Quickly and Accurately, downloadable here. This
also requires multiple-precision integer arithmetic

See also David M Gay's Correctly Rounded Binary-Decimal and
Decimal-Binary Conversions for algorithms going both ways.


--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.
You know you've been lisping too long when you see a recent picture of George
Lucas and think "Wait, I thought John McCarthy was dead!" -- Dalek_Baldwin

WJ

unread,
Jun 23, 2013, 1:23:42 PM6/23/13
to
Racket:

> (string->number "3.1415926536")
3.1415926536

WJ

unread,
Nov 12, 2014, 11:14:51 AM11/12/14
to
> > See also David M. Gay's Correctly Rounded Binary-Decimal and
> > Decimal-Binary Conversions for algorithms going both ways.
>
> Racket:
>
> > (string->number "3.1415926536")
> 3.1415926536

Gauche Scheme:

gosh> (string->number "3.1415926536")
3.1415926536


dinesh bhardwaj

unread,
Mar 29, 2018, 1:22:56 PM3/29/18
to
From Peter Norvig :

(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))))

and then add

(defun parse-number (str)
(if (search "." str)
(parse-float str)
(parse-integer str)))

now it can parse both float and integer. This should not be used where speed is concern.


On Wednesday, 10 January 1996 13:30:00 UTC+5:30, k p c wrote:
> Quoth ber...@vogelweide.uni-passau.de (Klaus Berndl):
> > Exists there a function or a macro in Common Lisp (Allegro CL 4.2)?
>

Robert L.

unread,
Mar 29, 2018, 5:07:04 PM3/29/18
to
On 3/29/2018, dinesh bhardwaj wrote:

> From Peter Norvig :
>
> (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))))
>
> and then add
>
> (defun parse-number (str)
> (if (search "." str)
> (parse-float str)
> (parse-integer str)))
>
> now it can parse both float and integer.

No, it can't.

* (parse-number "23 45 junk.")

23.45
5

--
Most Americans have no idea that Lincoln and [Karl] Marx corresponded.... When
Lincoln was re-elected in 1864, Marx sent a congratulatory letter to Lincoln
... and it basically says "We are fighting on the same side for the same
thing." http://archive.org/details/invaded

Robert L.

unread,
Mar 29, 2018, 5:17:16 PM3/29/18
to
On 3/29/2018, Robert L. wrote:

> On 3/29/2018, dinesh bhardwaj wrote:
>
> > From Peter Norvig :
> >
> > (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))))
> >
> > and then add
> >
> > (defun parse-number (str)
> > (if (search "." str)
> > (parse-float str)
> > (parse-integer str)))
> >
> > now it can parse both float and integer.
>
> No, it can't.
>
> * (parse-number "23 45 junk.")
>
> 23.45
> 5

* (parse-number "2345 junk")

debugger invoked on a SB-INT:SIMPLE-PARSE-ERROR in thread
#<THREAD "main thread" RUNNING {23EAC149}>:
junk in string "2345 junk"

--
If the Union was formed by the accession of States, then the
Union may be dissolved by the secession of States.
--- Daniel Webster, U.S. Senate, February 15, 1833
http://archive.org/details/invaded
0 new messages