View previous topic :: View next topic |
Author |
Message |
sush_sub Beginner
Joined: 02 May 2004 Posts: 2 Topics: 1 Location: India
|
Posted: Sun May 02, 2004 10:52 pm Post subject: Float point format conversion. |
|
|
I have an input file which give price in decimal format in a record of PIC X(14). The decimal point in this field can occur in any place Eg: it can be 123.45 or 123456789012.1 or 12.12345678987 etc...
I need to convert this number stored in file to the following format and write into another file:
Format 15.9 floating _________________ sush |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12380 Topics: 75 Location: San Jose
|
Posted: Mon May 03, 2004 9:04 am Post subject: |
|
|
Sush,
Try this code. I haven't fully tested it but you will get the idea
Code: |
WORKING-STORAGE SECTION.
01 W-IN-STR PIC X(14) VALUE '12.45 '.
01 W-FLD1 PIC X(14).
01 W-DEC-CNT PIC S9(04) COMP VALUE ZERO.
01 W-PTR PIC S9(04) COMP VALUE ZERO.
01 W-DEC-L PIC 9(2) VALUE ZERO.
01 W-INT-L PIC 9(2) VALUE ZERO.
01 W-PRD-POS PIC 9(2) VALUE ZERO.
01 W-INT-POS PIC 9(2) VALUE ZERO.
01 W-DEC-POS PIC 9(2) VALUE ZERO.
01 W-FINAL-STR PIC X(15) VALUE SPACES.
PROCEDURE DIVISION.
MOVE +1 TO W-PTR
MOVE '000000000000000' TO W-FINAL-STR
INSPECT FUNCTION REVERSE(W-IN-STR) TALLYING W-DEC-CNT
FOR LEADING SPACES
UNSTRING W-IN-STR DELIMITED BY '.'
INTO W-FLD1 WITH POINTER W-PTR
COMPUTE W-DEC-L = LENGTH OF W-IN-STR -
W-DEC-CNT - W-PTR + 1
COMPUTE W-INT-L = W-PTR - 2
COMPUTE W-PRD-POS = LENGTH OF W-FINAL-STR - W-DEC-L
COMPUTE W-INT-POS = W-PRD-POS - W-INT-L
COMPUTE W-DEC-POS = W-PRD-POS + 1
MOVE W-IN-STR(1 : W-INT-L) TO
W-FINAL-STR(W-INT-POS : W-INT-L)
MOVE '.' TO
W-FINAL-STR(W-PRD-POS : 1)
MOVE W-IN-STR(W-PTR : W-DEC-L) TO
W-FINAL-STR(W-DEC-POS : W-DEC-L)
DISPLAY 'FINAL STRING :' W-FINAL-STR
GOBACK.
|
Hope this helps...
Cheers
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12380 Topics: 75 Location: San Jose
|
Posted: Mon May 03, 2004 11:41 am Post subject: |
|
|
sush,
I just noticed that you wanted to round the result if the no: decimal digits is greater than 9. Add the following defnition in your working storage
Code: |
01 W-CHAR-DEC PIC X(09).
01 W-NUM-DEC REDEFINES W-CHAR-DEC PIC 9(09).
|
Add this piece of code just before the line COMPUTE W-PRD-POS =
Code: |
IF W-DEC-L > 9
MOVE W-IN-STR(W-PTR : W-DEC-L) TO W-CHAR-DEC
COMPUTE W-NUM-DEC ROUNDED = W-NUM-DEC + 1
MOVE +9 TO W-DEC-L
MOVE W-NUM-DEC TO W-IN-STR(W-PTR : 9)
END-IF
|
Hope this helps...
Cheers
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
 |
Cogito-Ergo-Sum Advanced
Joined: 15 Dec 2002 Posts: 637 Topics: 43 Location: Bengaluru, INDIA
|
Posted: Mon May 10, 2004 12:07 pm Post subject: |
|
|
Excuse me, but, isn't floating point represented in mantissa and exponent form? COMP-1 and COMP-2 in COBOL? _________________ ALL opinions are welcome.
Debugging tip:
When you have eliminated all which is impossible, then whatever remains, however improbable, must be the truth.
-- Sherlock Holmes. |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12380 Topics: 75 Location: San Jose
|
Posted: Mon May 10, 2004 4:39 pm Post subject: |
|
|
Cogito-Ergo-Sum,
Technically speaking you are right about floating point. But if you look at the question then you will find that the poster wanted a field with variable decimal point.
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
 |
neilxt Beginner
Joined: 01 Mar 2004 Posts: 23 Topics: 1
|
Posted: Tue May 11, 2004 4:37 pm Post subject: |
|
|
Compute Numeric-field = function numval(text-fild-containing-a-number). |
|
Back to top |
|
 |
sush_sub Beginner
Joined: 02 May 2004 Posts: 2 Topics: 1 Location: India
|
Posted: Wed May 12, 2004 1:25 am Post subject: |
|
|
The problem was that I was not able to declare a variable which will store the maximum decimal and integer part privide in the file. the maximum integer can be 13 digits and decimal can be 13 digits. But a PIC 9(13)v9(13) is invalid as it exeeds 18 bytes. I need to truncate extra zeroes in the decimal portion and prefix the integer part with zeroes so that the next length of number is 15.
Thus the numval function cant be used here. I can use the code which Kolusu had given. Now I have one more problem. I need to multiply the numeric value got from the file to another numeric value and store the result in the same format which I had given in my posting. For the purpose of multiplication I declared the source and destination variables as COMP-2. But I found that if we move a decimal number to COMP-2 say
89.456, it gets saved as 89.4559999 etc. I cannot store the numbers in COMP-3 as we cannot move a COMP-3 number to 9(7).9(3). What I tried to do was move the result of multiplication to two variables: one with PIC 9(14).9(3) and other with PIC 9(3).9(14) so that I can perform INSPECT statement and save the integer and decimal part of the number in required format.
But since the COMP-2 is changing the decimal portion of the number, I cannot use it. Does any body know about a numeric declaration which can store at least 14 bytes in integer and decimal and allow INSPECT variable to function on it ? _________________ sush |
|
Back to top |
|
 |
Mike Chantrey Intermediate
Joined: 10 Sep 2003 Posts: 234 Topics: 1 Location: Wansford
|
Posted: Wed May 12, 2004 9:34 am Post subject: |
|
|
If you have a recent compiler I think you can use the compilation option ARITH(EXTEND) to raise the limit from 18 to 31 digits. |
|
Back to top |
|
 |
|
|