Determine if a string is numeric: Difference between revisions

sql pl
(→‎{{header|Common Lisp}}: The previous numeric-string-p gave an error (in sbcl) when running and the string was not a number string. (Junk error). This version returns correctly NIL if a string contains junk.)
(sql pl)
Line 2,977:
string->number returns #f when the string is not numeric and otherwise the number, which is non-#f and therefore true.
<lang scheme>(define (numeric? s) (string->number s))</lang>
=={{header|SQL PL}}==
{{works with|Db2 LUW}} version 9.7 or higher.
With SQL PL:
<lang sql pl>
--#SET TERMINATOR @
 
CREATE OR REPLACE FUNCTION IS_NUMERIC (
IN STRING VARCHAR(10)
) RETURNS SMALLINT
-- ) RETURNS BOOLEAN
BEGIN
DECLARE RET SMALLINT;
-- DECLARE RET BOOLEAN;
DECLARE TMP INTEGER;
DECLARE CONTINUE HANDLER FOR SQLSTATE '22018'
SET RET = 1;
-- SET RET = FALSE;
 
SET RET = 0;
--SET RET = TRUE;
SET TMP = INTEGER(STRING);
RETURN RET;
END @
 
VALUES IS_NUMERIC('5')@
VALUES IS_NUMERIC('0')@
VALUES IS_NUMERIC('-1')@
VALUES IS_NUMERIC('A')@
VALUES IS_NUMERIC('-')@
VALUES IS_NUMERIC('z')@
VALUES IS_NUMERIC('')@
VALUES IS_NUMERIC(' ')@
</lang>
Output:
<pre>
db2 -td@
db2 => BEGIN
...
db2 (cont.) => END @
DB20000I The SQL command completed successfully.
 
VALUES IS_NUMERIC('5')
 
1
------
0
 
1 record(s) selected.
 
 
VALUES IS_NUMERIC('0')
 
1
------
0
 
1 record(s) selected.
 
 
VALUES IS_NUMERIC('-1')
 
1
------
0
 
1 record(s) selected.
 
 
VALUES IS_NUMERIC('A')
 
1
------
1
 
1 record(s) selected.
 
 
VALUES IS_NUMERIC('-')
 
1
------
1
 
1 record(s) selected.
 
 
VALUES IS_NUMERIC('z')
 
1
------
1
 
1 record(s) selected.
 
 
VALUES IS_NUMERIC('')
 
1
------
1
 
1 record(s) selected.
 
 
VALUES IS_NUMERIC(' ')
 
1
------
1
 
1 record(s) selected.
</pre>
 
=={{header|Racket}}==
<lang racket>(define (string-numeric? s) (number? (string->number s)))</lang>
Anonymous user