I want to compare the integer values of a decimal number. Fr eg - I want to compare 123 and 435 from 123.1212 and 435.3333.
Is there a way to get only integer value into a numeric field. I know the separate command but for that I need the input values as Alpha numeric. Also once separated the output will be in Alphanumeric field.
Natural has a function to extract the integer portion.
DEFINE DATA LOCAL
1 #N (N3.2) INIT <22.11>
1 #P (P2.3) INIT <22.222>
END-DEFINE
IF INT (#N) = INT (#P)
THEN
WRITE 'integer portions are equal' #N #P
END-IF
END
Page 1 16-11-27 23:29:21
integer portions are equal 22.11 22.222
Thanks so much. Never knew of the INT function. Your code is what I am exactly looking for. I used edit mask to move it to alpha numeric. Then used separate with delimiter ‘.’ But again I have it in alpha which I had to move to Numeric. But the below serves the purpose in a single step.
I am quite surprised that you would consider SEPARATE, but not the far more obvious code shown below:
DEFINE DATA LOCAL
1 #N (N3.2) INIT <22.11>
1 #P (P2.3) INIT <22.222>
1 #NI (I4)
1 #PI (I4)
END-DEFINE
*
COMPUTE #NI = #N
COMPUTE #PI = #P
IF #NI = #PI
THEN
WRITE ‘integer portions are equal’ #NI#PI
END-IF
END
I was not surprised you did not know about INT. There are MANY Natural facilities, functions, capabilities that remain unknown to the vast majority of Natural programmers (education, education, education).
One more note; many Natural functions are quite expensive. Following is a timing comparison.
1 #CPU-START (I4)
1 #CPU-ELAPSED (I4)
END-DEFINE
*
MOVE *CPU-TIME TO #CPU-START
SETA. SETTIME
FOR #LOOP = 1 TO 300000
IGNORE
END-FOR
COMPUTE #CPU-ELAPSED = *CPU-TIME - #CPU-START
WRITE 3/10 ‘CONTROL LOOP TIME==>’ *TIMD (SETA.) #CPU-ELAPSED /
*
MOVE *CPU-TIME TO #CPU-START
SETB. SETTIME
FOR #LOOP = 1 TO 300000
IF INT (#N) = INT (#P)
IGNORE
END-IF
END-FOR
COMPUTE #CPU-ELAPSED = *CPU-TIME - #CPU-START
WRITE 3/10 ‘INT FUNCTION TIME==>’ *TIMD (SETB.) #CPU-ELAPSED /
*
MOVE *CPU-TIME TO #CPU-START
SETC. SETTIME
FOR #LOOP = 1 TO 300000
COMPUTE #NI = #N
COMPUTE #PI = #P
IF #NI = #PI
IGNORE
END-IF
END-FOR
COMPUTE #CPU-ELAPSED = *CPU-TIME - #CPU-START
WRITE 3/10 ‘OWN INT FUNCTION TIME==>’ *TIMD (SETC.) #CPU-ELAPSED /
*
END
Page 1 16-11-28 06:50:29
CONTROL LOOP TIME==> 0 4
INT FUNCTION TIME==> 7 62
OWN INT FUNCTION TIME==> 2 20
As shown above, using the INT function is three times as expensive as writing your own integer function (times are PC times; mainframe times are typically proportionate to PC times).
Assuming your time is more valuable than the computer’s, just use
COMPUTE #NUM = VAL(#STRING)
This presupposes that #STRING only contains valid numeric data. If it doesn’t, you can use the IS test -
IF #STRING IS (N8) THEN
COMPUTE #NUM = VAL(#STRING)
END-IF
Note that the IS test just checks that #STRING can be converted to the format given. Thus “123.41” will be true for I4, N3 (or any large N4+), P3 or larger, N or P format with any number of decimals. If won’t be true for anything smaller - P1, P2, N1, N2 will be false.
Anyways, assuming simple case of only valid data in #STRING…
DEFINE DATA
LOCAL
01 #STRING1(A10) INIT<'123.41'>
01 #STRING2(A10) INIT<'123.23'>
01 #NUM1 (P7)
01 #NUM2 (P7)
END-DEFINE
*
#NUM1 := VAL(#STRING1)
#NUM2 := VAL(#STRING2)
IF #NUM1 = #NUM2
DISPLAY #NUM1 #NUM2
ELSE
WRITE T*#NUM1 #NUM1 T*#NUM2 'NOT EQUAL'
END-IF
END