How to test if string is numeric using Progress 4GL

Donna picture Donna · Oct 10, 2013 · Viewed 10.8k times · Source

Does Progress 4GL have a function for testing whether a string is numeric, like PHP's is_numeric($foo) function?

I've seen the function example at http://knowledgebase.progress.com/articles/Article/P148549 to test if a character in a string is numeric. Looks like it has a typo, btw.

But I would think the language would be a built-in function for this.

Answer

ns2016 picture ns2016 · Jan 11, 2017

I was looking at this myself recently. The approved answer given to this doesn't work in 100% situations.

If the user enters any of the following special string characters: ? * - or + the answer won't work.
A single plus or minus(dash) is converted to 0 which you may not want.
A single question mark character is valid value which progress recognises as unknown value at which again you may not want.
A single or group asterisks on their own also get converted to 0.
If you run the following code you'll see what I mean.

DISP DECIMAL("*")
     DECIMAL("**")
     DECIMAL("?")
     DECIMAL("+")
     DECIMAL("-").

The following additional code maybe useful to get around this

DEFINE VARIABLE iZeroCode    AS INTEGER   NO-UNDO.
DEFINE VARIABLE iNineCode    AS INTEGER   NO-UNDO.
DEFINE VARIABLE chChar       AS CHARACTER NO-UNDO.

ASSIGN iZeroCode = ASC("0")
       iNineCode = ASC("9")
       chChar    = SUBSTRING(cNumber,1,1).                           

IF NOT(ASC(chChar) >= iZeroCode AND ASC(chChar) <= iNineCode)    THEN DO:
    MESSAGE "Invalid Number..." VIEW-AS ALERT-BOX.
END.