Kbase 18447: How to evaluate if a value is numeric?
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  10/15/2008 |
|
Status: Verified
GOAL:
How to evaluate if a value is numeric?
GOAL:
How to verify if an expression is numeric?
CAUSE:
There being presently no 4GL function available to test whether a character field contains only numerics, the code below creates a stand-alone function to do this. It recognizes digits as well as the decimal point as numerics.
FIX:
/* isnumf.p */
DEFINE VARIABLE valnme AS CHARACTER FORMAT "X(60)" LABEL " Enter a value".
/* define function */
FUNCTION IsNum RETURNS LOGICAL (cValueString AS CHARACTER):
DEFINE VARIABLE iPos AS INTEGER NO-UNDO.
DEFINE VARIABLE iLength AS INTEGER NO-UNDO.
DEFINE VARIABLE cData AS CHARACTER NO-UNDO.
ASSIGN iLength = LENGTH(cValueString).
IF NUM-ENTRIES(cValueString,".") > 2 THEN
RETURN NO.
DO iPos = 1 TO iLength:
ASSIGN cData = SUBSTRING(cValueString,iPos,1).
IF cData < '0' OR cData > '9' THEN
IF (iPos = 1 AND INDEX("+-.",cData) = 0) OR (iPos > 1 AND cData <> ".") THEN
RETURN NO.
END.
RETURN YES.
END FUNCTION.
SET valnme.
IF IsNum(valnme) THEN
DISPLAY valnme "is all numeric".
ELSE
DISPLAY valnme "contains data other than numeric".
FIX:
References to Written Documentation:
Progress Solutions:
18400, "4GL. How To Be Sure Only Numbers Are Entered In Character Field"
14155, "How to detect non integer characters in field or variable"