View previous topic :: View next topic |
Author |
Message |
SathishKrishna Beginner
Joined: 18 Jun 2015 Posts: 3 Topics: 2
|
Posted: Fri Dec 18, 2015 5:56 am Post subject: Left justified alphanumeric to right justified numeric |
|
|
I need to convert Numbers stored as Text datatype(left aligned) ranging from 2 to 4 digits to Numeric format(Right aligned) padded with zeros in COBOL.
For eg, Code: |
Field1(Text left aligned) Field2(Numeric Right aligned)
------------------------------ -------------------------------
1000 001000
11 000011
-200 -00200
|
I tried with below options but my Cobol compiler is not supporting all.
NUMVAL
JUSTIFIED RIGHT
INSPECT with TRAILING SPACES
I appreciate any help on this. |
|
Back to top |
|
|
William Collins Supermod
Joined: 03 Jun 2012 Posts: 437 Topics: 0
|
Posted: Fri Dec 18, 2015 6:28 am Post subject: |
|
|
Well, I know of no Mainframe compiler which doesn't support JUST RIGHT.
INSPECT with TRAILING is non-Standard, and not an IBM Extension, so that won't work. I assume INSPECT itself does?
If you don't have FUNCTION NUMVAL you must have a very old COBOL compiler, like OS/VS COBOL. Or did you just miss out the word FUNCTION, or try to use it incorrectly?
You state 2-4 digits, but since you show a sign, do you mean maximum source size is five characters? And in what format do you want the output, actual leading zeros and a leading sign? |
|
Back to top |
|
|
kolusu Site Admin
Joined: 26 Nov 2002 Posts: 12359 Topics: 75 Location: San Jose
|
Posted: Fri Dec 18, 2015 11:14 am Post subject: |
|
|
SathishKrishna,
I am not sure as to why you are complicating a simple requirement. You are inconsistent with your zero padding for positive and negative numbers. Your final output size is 6 bytes of edited numeric data. For positive numbers you want a zero instead of the + sign and for the negative numbers you want the negative sign.
So use this
Code: |
WORKING-STORAGE SECTION.
01 INP-VAL PIC X(4).
01 OUT-VAL PIC -99999.
PROCEDURE DIVISION.
MOVE '1000' TO INP-VAL
IF INP-VAL > ' '
COMPUTE OUT-VAL = FUNCTION NUMVAL(INP-VAL)
ELSE
MOVE +0 TO OUT-VAL
END-IF
DISPLAY 'NUMERIC VALUE OF INP-VAL BEFORE : ' OUT-VAL
|
This will produce
The first byte is a space for the positive numbers. If you insist on having a zero for that then use this
Code: |
INSPECT OUT-VAL REPLACING LEADING ' ' BY '0'
DISPLAY 'NUMERIC VALUE OF INP-VAL AFTER : ' OUT-VAL
|
And this will give you the desired results
Code: |
001000
000011
-00200
|
_________________ Kolusu - DFSORT Development Team (IBM)
DFSORT is on the Web at:
www.ibm.com/storage/dfsort
www.linkedin.com/in/kolusu |
|
Back to top |
|
|
|
|