Re: Converting string to numerical data - regardless of errors
Posted in 1994
dennisp@informix.com (Dennis Pimple) writes: >If you insist on not doing via trapping, you could test each >character position >FUNCTION is_number(l_text) >DEFINE l_text CHAR(50) # Cope with looong numbers! >DEFINE x SMALLINT >IF LENGTH(l_text) = 0 THEN > # nothing to check > RETURN l_text >END IF >FOR x = 1 TO LENGTH(l_text) > IF l_text[x] NOT MATCHES "[0123456789]" THEN > # change any "bad" character to 0 > LET l_text[x] = "0" > END IF >END FOR >RETURN l_text >END FUNCTION This function does not allow for numbers smaller than 0. You should add '-' to the matching set (and posible a '.' if you want to include decimals). Also, rather than converting the "bad" characters to 0, I would copy the "good" characters to a char variable and then convert it and return an integer (or decimal) not a char. This way, if the text passed has commas (i.e. 1,000,000), it is converted correctly. Again: the return value should not be l_text but a numeric variable. >======================================================================= >Dennis J. Pimple dennisp@informix.com Opinions expressed >Senior Consultant -------------------- are mine, and do not >Informix Software Inc Voice: 303-850-0210 necessarily reflect >Denver Colorado USA Fax: 303-779-4025 those of my employer. -- Gabriel Beccar-Varela Internet : gabriel@netcom.com Voice : (510) 654-2030 Fax : (510) 654-3182