Question
· Jan 9, 2019

Numeric Function in Rule Editor

I would like to know if there is a built in function that checks a value in the rule editor to see if it is numeric.

Discussion (9)2
Log in or sign up to continue

There's no default function to do it, but you can easily develop your own custom function. There's $IsValidNum ObjectScript function, which could be used to check that variable is a number. So something like this should work:

/// Functions to use in rule definitions.
Class Custom.Functions Extends Ens.Rule.FunctionSet
{

/// Returns 1 if a string is a number, 0 otherwise
ClassMethod IsValidNumber(string As %String) As %Boolean [ CodeMode = expression, Final ]
{
$ISVALIDNUM(string)
}

}

The ClassMethod IsNumeric(...) seems to contain three SET statements that do nothing more than copy data around.  The following is equivalent (without data copies)

ClassMethod IsNumeric(value As %StringAs %Boolean
 { QUIT $ISVALIDNUM(value)  }

And this implementation of IsNumeric is equivalent to the first answer, ClassMethod  IsValidNumber(...) by  Eduard Lebedyuk except the IsNumeric ClassMethod does not have the [ FINAL ] attribute.

Here is a method we built for our use.

ClassMethod IsNumeric(value As %String) As %Boolean
{
//Load value into local var tNumber
set tNumber = value

//Use $extract to get the FULL VALUE into an internal format for $isvalidnum
set tFullValue = $EXTRACT(tNumber,1,$length(tNumber))

//Use $isvalidnum to check if the full value is numeric (ex: 123456789 = true, 123456789x = false)
set tNumberIsValid = $ISVALIDNUM(tFullValue)

//At this point, we simply return tNumberIsValid (it will either be 1, true or 0, false)
quit tNumberIsValid
}