View previous topic :: View next topic |
Author |
Message |
anita_m Beginner
Joined: 20 Sep 2006 Posts: 41 Topics: 12 Location: Venus
|
Posted: Wed May 23, 2007 8:23 pm Post subject: data type conversion in cobol |
|
|
How can I format a PIC 9(9) field into PIC 9(5)V9(4) COMP-3 in cobol. |
|
Back to top |
|
|
kolusu Site Admin
Joined: 26 Nov 2002 Posts: 12375 Topics: 75 Location: San Jose
|
Posted: Thu May 24, 2007 10:03 am Post subject: |
|
|
anita_m,
Is this a trick question?
a Simple move
MOVE <PIC 9(9)> TO <PIC 9(5)V9(4) COMP-3>
will solve it.
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
|
Nic Clouston Advanced
Joined: 01 Feb 2007 Posts: 1075 Topics: 7 Location: At Home
|
Posted: Thu May 24, 2007 10:14 am Post subject: |
|
|
Kolusu - there were loads of responses here earlier - Dick and I both had responses. Dick posed the Q. does the PIC9(9) field have an implied decimal point in it? In which case a division by a power of 10 would be needed during the assignment. _________________ Utility and Program control cards are NOT, repeat NOT, JCL. |
|
Back to top |
|
|
dbzTHEdinosauer Supermod
Joined: 20 Oct 2006 Posts: 1411 Topics: 26 Location: germany
|
Posted: Thu May 24, 2007 10:28 am Post subject: |
|
|
Nic, check the dinosaur forum. _________________ Dick Brenholtz
American living in Varel, Germany |
|
Back to top |
|
|
Nic Clouston Advanced
Joined: 01 Feb 2007 Posts: 1075 Topics: 7 Location: At Home
|
Posted: Thu May 24, 2007 10:31 am Post subject: |
|
|
I think I got here whilst it was in transit because there was nothing flagged there when I came in here. _________________ Utility and Program control cards are NOT, repeat NOT, JCL. |
|
Back to top |
|
|
anita_m Beginner
Joined: 20 Sep 2006 Posts: 41 Topics: 12 Location: Venus
|
Posted: Thu May 24, 2007 11:30 am Post subject: |
|
|
No 9(9) does not have an implied decimal.
Assuming IN-VAR -> 118030000
Code: |
IN-VAR PIC 9(9).
WS-VAR PIC 9(9).
WS-VAR-R REDEFINES WS-VAR PIC 9(5)V9(4).
OUT-VAR PIC 9(5)V9(4) COMP-3.
MOVE IN-VAR TO WS-VAR.
MOVE WS-VAR-R TO OUT-VAR.
DISPLAY 'OUT-VAR' -> 118030000
|
Code: |
MOVE IN-VAR TO OUT-VAR.
DISPLAY 'OUT-VAR' -> 300000000
|
|
|
Back to top |
|
|
Nic Clouston Advanced
Joined: 01 Feb 2007 Posts: 1075 Topics: 7 Location: At Home
|
Posted: Thu May 24, 2007 11:39 am Post subject: |
|
|
In fact IN-VAR does have an 'IMPIED' decimal because WS-VAR-R is redefing IN-VAR to be 5.4 so by using WS-VAR-R your 118030000 becomes 11803.0000. by moving IN-VAR to OUT-VAR you are moving 9(9)V to 9(5)V9(4). This can only handle up to 99999.9999 so your high-order bytes are being dropped hence the 30000000 which is really 30000.0000. If OUT-VAR was a DISPLAY variable wth the decimal point coded then it would be displayed as 30000.0000. Hope I got the right number of zeros in all that! _________________ Utility and Program control cards are NOT, repeat NOT, JCL. |
|
Back to top |
|
|
Phantom Data Mgmt Moderator
Joined: 07 Jan 2003 Posts: 1056 Topics: 91 Location: The Blue Planet
|
Posted: Thu May 24, 2007 11:40 am Post subject: |
|
|
Anita_M,
You have shown two different ways of converting yourself. Now what is your actual query ??? If you wanted the last 4 digits of 9(09) to be considered as decimals then the REDEFINES option or DIVISION by 10000 would solve the problem.
Else, you would get truncated result - when you move 9(09) to 9(5)V9(4). Both are correct - as per the rules of COBOL.
So, what is your question ?
Thanks,
Phantom |
|
Back to top |
|
|
anita_m Beginner
Joined: 20 Sep 2006 Posts: 41 Topics: 12 Location: Venus
|
Posted: Thu May 24, 2007 2:06 pm Post subject: |
|
|
When IN-VAR = 118030000
MOVE IN-VAR TO OUT-VAR.
DISPLAY 'OUT-VAR' -> 300000000
I wanted to know the reason. |
|
Back to top |
|
|
kolusu Site Admin
Joined: 26 Nov 2002 Posts: 12375 Topics: 75 Location: San Jose
|
Posted: Thu May 24, 2007 2:15 pm Post subject: |
|
|
Quote: |
When IN-VAR = 118030000
MOVE IN-VAR TO OUT-VAR.
DISPLAY 'OUT-VAR' -> 300000000
I wanted to know the reason.
|
anita_m,
anita_m,
are u serious ? You are trying to move 9 byte field into a 5 byte. Numeric values are moved to from right to left. so the last 5 bytes of input are stored in the first 5 bytes of the output.
Code: |
1 2 3 4 5 6 7 8 9
=========================
1 1 8 0 3 0 0 0 0
=========================
1 2 3 4 5
|
If you really want to store 4 decimals then try this
Code: |
01 IN-VAR PIC 9(09).
01 OUT-VAR PIC S9(05)V9(4) COMP-3.
MOVE 118030000 TO IN-VAR
COMPUTE OUT-VAR = IN-VAR / 10000
DISPLAY 'OUT-VAR' OUT-VAR
|
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
|
Phantom Data Mgmt Moderator
Joined: 07 Jan 2003 Posts: 1056 Topics: 91 Location: The Blue Planet
|
Posted: Thu May 24, 2007 2:17 pm Post subject: |
|
|
Good question....
You need to understand the basics. Though IN-VAR and OUT-VAR are defined to have 9 digits (on total), COBOL processes the Implied decimal provided in the declaration of OUT-VAR (9(05)V9(04)) carefully and knows that It can hold max of 5 numeric digits.
But you are trying to move 9 numeric digits to OUT-VAR, so the truncation happens. It actually moved last 5 digits which is '30,000' to numeric portion of OUT-VAR. Now, since there are no decimal places in the source field, COBOL puts the default value of '0' in the decimal portion of the receiving field.
So, it pads 4 decimals after '30,000' - Now you get '30,000.0000' which COBOL displays as 300000000 - since you did not use any EDIT characters.
Try moving OUT-VAR to some edited variable in the format ZZ,ZZZ.9999 and then see the displays.
Hope this answers your question
Thanks,
Phantom |
|
Back to top |
|
|
|
|