Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to convert a cobol numb

957 views
Skip to first unread message

Benedikt Gerlich

unread,
Jan 18, 2003, 5:14:36 PM1/18/03
to
Hello,
is there a way to automatically convert a number in cobol to a string
which also contains a decimal sign.

For Example:

01 TEST-NUMBER PIC S9(3)V9(3).
01 TEST-STRING PIC X(10).

MOVE 123,456 TO TEST-NUMBER.
MOVE TEST-NUMBER TO TEST-STRING.

Now I want to get "123,456" in TEST-STRING and not "123456" like Cobol
does it normally.

Normally we do it hard-coded like this
MOVE TEST-NUMBER(1:3) TO TEST-STRING(1:3).
MOVE "," TO TEST-STRING(4:1).
MOVE TEST-NUMBER(4:3) TO TEST-STRING(5:3).

but this isn't very flexible and the code must be changed everytime
the variable length is changed.

Best regards
Benedikt Gerlich

ScLind

unread,
Jan 18, 2003, 11:24:57 PM1/18/03
to
>For Example:
>
>01 TEST-NUMBER PIC S9(3)V9(3).
>01 TEST-STRING PIC X(10).
>
>MOVE 123,456 TO TEST-NUMBER.
>MOVE TEST-NUMBER TO TEST-STRING.
>
>Now I want to get "123,456" in TEST-STRING and not "123456" like Cobol
>does it normally.

My COBOL is a bit rusty, but I think all you want is:

01 TEST-STRING PIC 999.999
(or ZZ9.999) if you want zero suppression.

I'm used to "." for a decimal point, but I seem to rememer a phrase "DECIMAL
POINTS IS COMMA" that might apply.

You might want to check or ask in comp.lang.cobol.

Scott

Phil

unread,
Jan 19, 2003, 5:53:47 AM1/19/03
to

"Benedikt Gerlich" <b.ge...@online.de> a écrit dans le message de news: 2148cba6.03011...@posting.google.com...


Benedikt,

though my cobol is also a bit rusty, you might try this or something similar.

77 WRK-NBR PIC X(30).
77 WRK-NBR-INT PIC 9(15) COMP
77 WRK-NBR-DEC PIC 9(15) COMP.
...
01 TEST-NBR PIC X(30).
....

MOVE SPACES TO WRK-NBR.
MOVE YOUR-NBR TO WRK-NBR..

* If 'YOUR-NBR' is equal to 123V456 'WRK_NBR-INT' would contain the Number of interger positions
* and 'WRK_NBR-DEC' the number of decimal positions

MOVE 3 TO WRK_NBR-INT.
MOVE 3 TO WRK_NBR-DEC.

* You may transform this subroutine in an external module as shown here
* CALL 'CONV' USING WRK-NBR WRK-NBR-INT WRK-NBR-DEC TEST-STRING

PERFORM CONV THRU CONV-EXIT.

MOVE TEST-STRING TO .....
......

CONV.

MOVE SPACES TO TEST-STRING..
MOVE TEST-NBR(1:WRK_NBR-INT) TO TEST-STRING(1:WRK_NBR-INT).
MOVE "," TO TEST-STRING(WRK_NBR-INT+1 : 1).
MOVE TEST-NBR(WRK_NBR-INT+2:WRK-NBR-DEC) TO TEST-STRING(WRK_NBR-INT+2: WRK_NBR-DEC).

CONV-EXIT.
EXIT.

HTH
Phil


Terry Winchester

unread,
Jan 19, 2003, 8:36:30 AM1/19/03
to
On 18 Jan 2003 14:14:36 -0800, b.ge...@online.de (Benedikt Gerlich)
wrote:

Since your value could be signed you should use
a signed edit field.

01 TEST-NUMBER PIC S9(3)V9(3).
01 TEST-STRING PIC X(10).

01 Edit-Nbr pic 999.999-.

move TEST-NUMBER to Edit-Nbr.
move Edit-Nbr to TEST-STRING.

This should be all that's needed unless
you need zero-suppression. If so, then
change your edit field to be: ZZ9.999-

I assume, since your European, that the
default value for the decimal point is
the comma. If not, you'll also have to
use the "DECIMAL POINT IS COMMA" in your
program (as someone else already indicated).

HTH,
Terry

Benedikt Gerlich

unread,
Jan 19, 2003, 6:38:49 PM1/19/03
to
Thanks for all the help but I need a solution where I don't need to
hard-code the length of the number like done below with 01 Edit-Nbr
pic 999.999-.
Our Variables are defined with the LIKE statement out from database
files.

So when the length in the database file changes I dont't want to
change anything in the code except recompiling it.

Terry Winchester <terrywi...@pronetisp.net> wrote in message news:<72al2vkri4449qlpu...@4ax.com>...

0 new messages