Question Jonathan Harris · Oct 24, 2024

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

Comments

Jonathan Harris · Oct 24, 2024

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

0
Alexander Pettitt · Oct 24, 2024

You could optimize this:

If F1 > 0 and F2 > 0

F1 > F2 ; positive

F1 < F2 ; negative

no need do the subtraction.

0
Jonathan Harris · Oct 24, 2024

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.

0
Enrico Parisi  Oct 25, 2024 to Jonathan Harris

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

0
Timo Lindenschmid · Oct 28, 2024

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"
}
0
Enrico Parisi  Oct 28, 2024 to Timo Lindenschmid

True, but the problem definition is:

If both fields are numeric and 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.

0
Timo Lindenschmid  Oct 29, 2024 to Enrico Parisi

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

0
Enrico Parisi  Oct 29, 2024 to Timo Lindenschmid

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
0
Jonathan Harris · Oct 28, 2024

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

0