Check if char string is integer

nate100

Member
How do I check to see if a character string is integer.
For example, I want to check to see that "134343" is integer or not.
Is there a simple way of doing this.?
 

Osborne

Active Member
Code:
DEF VAR vNumber AS INT.
DEF VAR vCharString AS CHAR INITIAL "134343".
 
ASSIGN vNumber = INT(vCharString) NO-ERROR.
IF ERROR-STATUS:ERROR THEN
   MESSAGE "Character String is not an integer." VIEW-AS ALERT-BOX INFORMATION.
ELSE
   MESSAGE "Character String is an integer." VIEW-AS ALERT-BOX INFORMATION.
 
Code:
DEF VAR vNumber AS INT.
DEF VAR vCharString AS CHAR INITIAL "134343".
 
ASSIGN vNumber = INT(vCharString) NO-ERROR.
IF ERROR-STATUS:ERROR THEN
   MESSAGE "Character String is not an integer." VIEW-AS ALERT-BOX INFORMATION.
ELSE
   MESSAGE "Character String is an integer." VIEW-AS ALERT-BOX INFORMATION.


This approach has a couple of obscure gotchas, eg.
try it with vCharString = '-'.

Best way is to parse and check each character, though you may be able to use the above method if you trim out certain characters first.
 

sdjensen

Member
Code:
function myinteger returns integer (str as char):
  define variable str2 as character no-undo.
  define variable i as integer no-undo.
  str= trim(str).
  do i = 1 to length(str):
    str2 = substring(str,i,1).
    if str2 = '-' then do:
      if i <> 1 then
        return  ?.
      else
        next.
    end.
    if lookup(str2, '0,1,2,3,4,5,6,7,8,9,0') = 0 then
      return  ?.
  end.
  return integer(str).
end function.

If function returns ? then input is not integer.
 

cibulka

New Member
FUNCTION isInteger RETURNS LOGICAL( cInteger AS CHARACTER) :
DEFINE VARIABLE iInteger AS INTEGER NO-UNDO.
iInteger = INTEGER(cInteger) NO-ERROR.
IF ERROR-STATUS:ERROR THEN
RETURN FALSE.
IF STRING(iInteger) NE cInteger THEN
RETURN FALSE.
RETURN TRUE.
END FUNCTION.
 

sunilnair

Member
If none of the pieces of code given above work ... you can probably use the ASCII value .....

ASC('0') = 48
ASC('1) = 49
--
--
--
--
--
--
--
ASC('9') = 57.
This is a fool proof way to check whether the characters in your string are all integers or if there is a special character in there . Because Error-Status('-') will also cause an error.

Cheers
Sunil.
 
FUNCTION isInteger RETURNS LOGICAL( cInteger AS CHARACTER) :
DEFINE VARIABLE iInteger AS INTEGER NO-UNDO.
iInteger = INTEGER(cInteger) NO-ERROR.
IF ERROR-STATUS:ERROR THEN
RETURN FALSE.
IF STRING(iInteger) NE cInteger THEN
RETURN FALSE.
RETURN TRUE.
END FUNCTION.


Nice, but you arguably need a trim.

MESSAGE isinteger(' 90')
VIEW-AS ALERT-BOX INFO BUTTONS OK.

And what about

MESSAGE isinteger(?)
VIEW-AS ALERT-BOX INFO BUTTONS OK.
 
Code:
function myinteger returns integer (str as char):
  define variable str2 as character no-undo.
  define variable i as integer no-undo.
  str= trim(str).
  do i = 1 to length(str):
    str2 = substring(str,i,1).
    if str2 = '-' then do:
      if i <> 1 then
        return  ?.
      else
        next.
    end.
    if lookup(str2, '0,1,2,3,4,5,6,7,8,9,0') = 0 then
      return  ?.
  end.
  return integer(str).
end function.

If function returns ? then input is not integer.

But MESSAGE myinteger('-')
VIEW-AS ALERT-BOX INFO BUTTONS OK...
 

cibulka

New Member
Nice, but you arguably need a trim.

MESSAGE isinteger(' 90')
VIEW-AS ALERT-BOX INFO BUTTONS OK.

And what about

MESSAGE isinteger(?)
VIEW-AS ALERT-BOX INFO BUTTONS OK.

MESSAGE isinteger(' 90')
VIEW-AS ALERT-BOX INFO BUTTONS OK.

It depend if leading space sholud be interpreted as correct integer or not.

MESSAGE isinteger(?)
VIEW-AS ALERT-BOX INFO BUTTONS OK.

Yes , you right, unknown value shold be treated.
 
MESSAGE isinteger(' 90')
VIEW-AS ALERT-BOX INFO BUTTONS OK.

It depend if leading space sholud be interpreted as correct integer or not.

That is why I rather weaselly included 'arguably' in my post, but I suspect a user entering ' 90' in an input field would be suprised if it was rejected as a number.

MESSAGE isinteger(?)
VIEW-AS ALERT-BOX INFO BUTTONS OK.

Yes , you right, unknown value shold be treated.

Though some difficult types might argue that ? is a valid member of the Progress integer set.:read:

Not me though. Well, not today.
 
Using cibulkas method, how about the unreadable:

Code:
FUNCTION isInteger RETURNS LOGICAL( cInteger AS CHARACTER) :

DEF VAR lTest AS LOGICAL NO-UNDO.
ASSIGN lTest = (cInteger NE ?) AND 
               (STRING(INTEGER(cInteger)) EQ TRIM(cInteger)) NO-ERROR.

RETURN lTest.

END FUNCTION.

I was trying to get it down to a geeky one line, but couldn't bypass errors 76 and 77, hence the variable and assign. Perhaps someone else can...

What was that I was saying in another thread about time well spent?
 

cibulka

New Member
Using cibulkas method, how about the unreadable:

Code:
FUNCTION isInteger RETURNS LOGICAL( cInteger AS CHARACTER) :

DEF VAR lTest AS LOGICAL NO-UNDO.
ASSIGN lTest = (cInteger NE ?) AND 
               (STRING(INTEGER(cInteger)) EQ TRIM(cInteger)) NO-ERROR.

RETURN lTest.

END FUNCTION.
I was trying to get it down to a geeky one line, but couldn't bypass errors 76 and 77, hence the variable and assign. Perhaps someone else can...

What was that I was saying in another thread about time well spent?

I've add LEFT-TRIM


FUNCTION isInteger RETURNS LOGICAL( cInteger AS CHARACTER) :

DEF VAR lTest AS LOGICAL NO-UNDO.
ASSIGN lTest = (cInteger NE ?) AND
(STRING(INTEGER(cInteger)) EQ LEFT-TRIM(TRIM(cInteger), "0":U)) NO-ERROR.

RETURN lTest.

END FUNCTION.

because of

MESSAGE isInteger(" 0123")
VIEW-AS ALERT-BOX INFO BUTTONS OK.
 
I've add LEFT-TRIM


FUNCTION isInteger RETURNS LOGICAL( cInteger AS CHARACTER) :

DEF VAR lTest AS LOGICAL NO-UNDO.
ASSIGN lTest = (cInteger NE ?) AND
(STRING(INTEGER(cInteger)) EQ LEFT-TRIM(TRIM(cInteger), "0":U)) NO-ERROR.

RETURN lTest.

END FUNCTION.

because of

MESSAGE isInteger(" 0123")
VIEW-AS ALERT-BOX INFO BUTTONS OK.

And now what about MESSAGE isInteger("0")
VIEW-AS ALERT-BOX INFO BUTTONS OK?

:)
 

sdjensen

Member
Code:
function myinteger returns integer (str as char):
  define variable str2 as character no-undo.
  define variable i as integer no-undo.
  str= trim(str).
  if str = '-' or str = '' then
    return ?.
  do i = 1 to length(str):
    str2 = substring(str,i,1).
    if str2 = '-' then do:
      if i <> 1 then
        return  ?.
      else
        next.
    end.
    if lookup(str2, '0,1,2,3,4,5,6,7,8,9,0') = 0 then
      return  ?.
  end.
  return integer(str).
end function.

Now it works with myinteger('') and myinteger(?).
 
Code:
function myinteger returns integer (str as char):
  define variable str2 as character no-undo.
  define variable i as integer no-undo.
  str= trim(str).
  if str = '-' or str = '' then
    return ?.
  do i = 1 to length(str):
    str2 = substring(str,i,1).
    if str2 = '-' then do:
      if i <> 1 then
        return  ?.
      else
        next.
    end.
    if lookup(str2, '0,1,2,3,4,5,6,7,8,9,0') = 0 then
      return  ?.
  end.
  return integer(str).
end function.

Now it works with myinteger('') and myinteger(?).

You have a redundant '0' in your lookup.
 
Top