Check for numeric value in variable

Potish

Member
I should know this by now but I don't.

What's the best way to check if the value in a char variable is an integer or decimal?
 
"Best" depends on your criteria. Which you haven't stated...

I like to do things like this:
Code:
define variable x as decimal no-undo.
x = decimal( "x113.56" ) no-error.
if error-status:num-messages > 0 then
  message "oops!".
 else
  message "ok".
 
I've not tested this, but it should give you the right idea. Tom's code tells you if something is numeric. It needs a little enhancing to tell you if it is integer, or decimal.

Code:
def var x as decimal no-undo.
x = decimal("111.33") no-error. 
if error-status:num-messages gt 0 then 
  message "oops!". 
else 
do:
  if truncate(x,0) ne x then 
    message "decimal". 
  else 
    message "integer". 
end.
 
Thank you for the suggestions. I should be able to work with one of these.

Just to clarify the variable is a char type and is used to read input from a data file. That data can either be char or integer and I need to identify the integer values as they require some additional processing.
 
Back
Top