Anonymous user
Determine if a string is numeric: Difference between revisions
→{{header|Common Lisp}}: numberp is insufficient as it doesn't take strings; added 2 parsing options
(numberp) |
(→{{header|Common Lisp}}: numberp is insufficient as it doesn't take strings; added 2 parsing options) |
||
Line 217:
=={{header|Common Lisp}}==
If the input may be relied upon to not be malicious, then it may be ''read'' and the result checked for being a number.
<lang lisp>(
(numberp (read-from-string string)))</lang>
However, <code>read</code>[<code>-from-string</code>] parses more than just numbers, and some input can have side effects and/or large memory allocation. The [http://www.cliki.net/PARSE-NUMBER <code>parse-number</code>] library provides a numbers-only equivalent of <code>read</code>.
<lang lisp>(defun numeric-string-p (string)
(handler-case (progn (parse-number:parse-number string)
t) ; parse succeeded, discard it and return true (t)
(parse-number::invalid-number ()
nil))) ; parse failed, return false (nil)</lang>
=={{header|D}}==
|