From: Jeff Dalton

Subject: Re: ask about the common LISP function about conversion

Date: 1999-4-27 13:33

For converting numbers to strings, the various output functions are
probably the best way to do it.  I'd probably use write-to-string
rather than princ1-to-string or princ-to-string, but I don't think it
matters very much.  Format will give you the most control over what
the string looks like.

For example: (prin1-to-string pi) returns "3.141592653589793d0"
in Allegro 5.0, but (format nil "~,4f" pi) returns "3.1416".
So format lets you control such things as how many digits
there should be after the decimal point.

To convert strings to numbers, you can use read-from-string,
but it will also read non-numbers, so you need to check the
result.  There can also be security implications, because
the string might contain #. and #.expression causes the
expression to be evaluated at read-time.  So it's not safe
to use read-from-string for such things as converting query
args in CGI programs (unless you disable #.).

Also, for converting strings to integers, it's better to use
parse-integer, since it's intended for precisely that case.

So, for all of theses reasons, I use the following to convert
a string to an integer:

(defun string->int (s)
  (handler-case (values (parse-integer s))
    (error (c)
      (error "Can't convert ~S to an int because: ~A" s c))))

-- jeff