How to test if string is numeric using Progress 4GL
Asked Answered
F

5

5

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.

Flagellum answered 10/10, 2013 at 13:53 Comment(1)
If you have a Windows based Progress install you probably have the help available. It's the "?" icon in the program group off the start menu. The best part of it is the reference manual. It's really quite easy to search through it to see if a function exists in the language -- or you can just scroll through the table of contents and see if anything catches your eye. It's a great way to learn about things that you didn't know were there.Gut
U
5

Do not need a function can jsut do a straight conversion.

ASSIGN dNumber = DECIMAL(cNumber) NO-ERROR. 
IF ERROR-STATUS:ERROR THEN
DO:
    {Handle issues}        
END.

or if it is always whole numbers can use INTEGER instead of DECIMAL.

Urology answered 11/10, 2013 at 6:42 Comment(2)
Of course you don't need a function -- but she did ask for one.Gut
true, but she asked for a built-in function ;)Urology
D
5

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.
Droppings answered 11/1, 2017 at 19:41 Comment(0)
G
4

The language does not have a built-in "isNum()" type of function.

An alternative to the kbase method would be:

function isNum returns logical ( input s as character ):
  define variable n as decimal no-undo.
  assign
    n = decimal( s )
    no-error
  .
  return ( error-status:num-messages = 0 ).
end.

display isNum( "123" ) isNum( "xyz" ).
Gut answered 10/10, 2013 at 14:38 Comment(0)
W
2

This code handles any numeric strings - even if the used Character is longer than the max Decimal length etc.

FUNCTION isNumeric RETURNS LOGICAL (textvalue AS CHAR):
    DEF VAR i AS INT NO-UNDO.

    IF textvalue = ? THEN RETURN TRUE.

    DO i = 1 TO (LENGTH(textvalue) - 1):
        INT(SUBSTRING(textvalue, i, (i + 1))) NO-ERROR.
        IF ERROR-STATUS:ERROR THEN RETURN FALSE.
    END.

    RETURN TRUE.
END FUNCTION.
Wergild answered 11/11, 2016 at 12:1 Comment(0)
S
1

Works 100% of the time

FUNCTION is-num RETURNS LOGICAL
    (INPUT cString AS CHARACTER):
    DEFINE VARIABLE iZeroCode    AS INTEGER   NO-UNDO.
    DEFINE VARIABLE iNineCode    AS INTEGER   NO-UNDO.
    DEFINE VARIABLE cChar        AS CHARACTER NO-UNDO.
    DEFINE VARIABLE iCount       AS INTEGER   NO-UNDO.

    DO iCount = 1 TO LENGTH(cString):
        ASSIGN iZeroCode = ASC("0")
               iNineCode = ASC("9")
               cChar     = SUBSTRING(cString,iCount,1).                           

        IF NOT(ASC(cChar) >= iZeroCode AND ASC(cChar) <= iNineCode)    THEN DO:
            RETURN FALSE.
        END.
    END.
    RETURN TRUE.
END.
Simmon answered 28/4, 2020 at 8:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.