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.
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.
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.
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.
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.
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.
Using cibulkas method, how about the unreadable:
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...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.
What was that I was saying in another thread about time well spent?
.....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.
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.
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(?).