Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determining the last digit of a packed number 2

Status
Not open for further replies.

lbzh

Programmer
Aug 7, 2004
25
US
I have a field as below:

4 Field1 PIC 9(5) COMP-3.


What is the simplest way to extract the last digit of Field1?

Example

Field1 Last Digit
12345 5
45678 8

Field1 is always greater than 9999 and is always less than 100,000.

Thank You.
 
01 Field1 PIC 9(5) COMP-3.
01 Field2 PIC 9(5).
01 FILLER REDEFINES Field2.
03 FILLER PIC X(4).
03 Last9 PIC 9.

...
MOVE Field1 TO Field2.
DISPLAY Last9.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
field-1 pic 9(5) comp-3.
field-2 pic 9.
move field-1 to field-2.
 
Why not use a redefinition to avoid any moves at all?
Code:
4 field-1              pic 9(5) comp-3.
4 redefines field-1.
  5                    pic xx.
  5 field-1-last-digit pic 9(1) comp-3.

Note that this is not a general purpose solution, but should suffice in most cases.

Regards.

Glenn
 
Glenn's solution will work on IBM mainframe compilers and many (most?) other compilers with COMP-3 as Packed-Decimal support. HOWEVER, it will *not* work on all non-IBM compilers. Some vendors do *NOT* allocate a "sign-nibble" for unsigned COMP-3 fields, i.e. for

PIC 9(5) Comp-3

the last byte includes the last *2* digits (with no sign to indicate it is "unsigned".

Personally, I prefer the "MOVE to 1 display numeric digit" solution as it will ALWAYS work. (Truncation is "guaranteed")

Bill Klein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top