Question
Jonathan Harris · Oct 24

# I want to test if two fields are numeric and positive, do subtraction, and test if the result is positive.

If both fields are numeric and the result of subtraction of field1-field2 is positive, only then put the result is a data field.

I am doing this within an Iris DTL.

I don't find any functions like IsNumeric(). Once I get that, I can test if field1>0 and field2>0, do the subtraction, and test if diff>0.

I just need a function to determine if they are numeric, rather than some cumbersome way like a regex where the only characters are 0-9.

I see functions in documentation but don't see them used at tests, only in WRITE statements.

Thanks,

Jonathan

**Product version:**IRIS 2023.3

As Emily Latella would say, "Never mind." I tried the $NUMBER function and it worked.

You could optimize this:

If F1 > 0 and F2 > 0

F1 > F2 ; positive

F1 < F2 ; negative

no need do the subtraction.

To check if MyVal is a valid positive number I'd use $ISVALIDNUM(MyVal,,0)

See $ISVALIDNUM function documentation.

Now there is an issue. When a data field is 0, it does not read it as numeric.

Example, I have input values of 3.5 and 0 and want to subtract them, but I don't get output

Example, I have input values of 3.5 and 1 and want to subtract them, output is 2.5.

Can you please provide some details on what you are actually doing in your DTL?

pure objectscript way no functions needed, we just use the IRIS type conversion behavior:

`set number1="123123" set number3="123123123 Not a pure number" if ((+number1=number1) && (+number2=number2) && (number1>=number2)) { set div=number1-number2 } else { w !,"Conditions not met" }`

True, but the problem definition is:

If both fields are numericand the result of subtraction of field1-field2 is positive.My understanding/interpretation is that the subtraction and further test should be performed only "If both fields are numeric", so the code should check that condition.

If the first part of the problem (If both fields are numeric) is irrelevant....then the question/problem definition is misleading.

Hi Enrico, my sample code does that exactly. It takes number1 and converts it to a number than it compares the result to the original number1 field.

Quirk of type conversion in ObjectScript if a value is converted from string to number ObjectScript walks the values from left to right and stops at the first non numeric character ie.

123ABC becomes 123

The if clause checks then if 123 = 123ABC so the org value is non numeric.

Using && in the IF clause means do a logical AND check but short circuit the check ie. if condition 1 is already false go directly to the else clause and do not check cond2 and3

Hi Timo, your sample is not a valid/full check for "valid number":

`USER>set number="123.40" USER>write (+number=number) 0 USER>write $isvalidnum(number) 1`

I figured it out with the ISVALIDNUM function, so all is good.