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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting Alphanumeric Characters to Decimal

Status
Not open for further replies.

waycon

Technical User
Sep 4, 2002
17
0
0
US
What is the best way to convert an X(8)field to a 9(8)field? Simply moving the X type to the 9 type causes a justification problem.

I am using Fujitsu COBOL, not Power Cobol.
 
How does your MOVE statement that's causing problems look like?

Have you tried:

MOVE varx (1:8) TO var9 (1:8)
 
Hi Waycon,

The most simple would be:

03 VARX PIC X(08).
03 VAR9 REDEFINES VARX PIC 9(08).
03 TARGETVAR9 PIC 9(08).

and MOVE VAR9 instead of VARX TO TARGETVAR9.

Or do you mean something else? Marcel
 
This is in POWER COBOL V.7.0 but it's the same thing
use FUNCTION NUMVAL it's easy
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CONTROLLI.
03 XDAT PIC X(10).
03 NDAT PIC 9(10).
PROCEDURE DIVISION.
A.
MOVE SPACE TO XDAT

MOVE 0 TO NDAT
MOVE "123,00" TO XDAT
COMPUTE NDAT = FUNCTION NUMVAL(XDAT)

EXIT PROGRAM.

fsccdm
 
Dimandja,

You were the first to reply so I am trying your suggestion first. I wrote a small test program. The result was
100010000. Where am I going wrong?

IDENTIFICATION DIVISION.
PROGRAM-ID. TEST1.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 VARX PIC X(20) VALUE "1000".
01 VAR9 PIC 9(9) VALUE 0.
01 10K PIC 9(9) VALUE 10000.
01 RESULT PIC 9(9) VALUE 0.
PROCEDURE DIVISION.
MOVE VARX (1:9) TO VAR9 (1:9).
SUBTRACT VAR9 FROM 10K GIVING RESULT.
DISPLAY RESULT.
END PROGRAM TEST1.

 
Marcel,

I tried your example with the following results. Obviuosly, I'm overlooking something.

IDENTIFICATION DIVISION.
PROGRAM-ID. TEST1.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 VARX PIC X(09).
01 VAR9 REDEFINES VARX PIC 9(09).
01 10K PIC 9(09).
01 RESULT PIC 9(09).
PROCEDURE DIVISION.
MOVE "1000" TO VARX.
MOVE VARX TO VAR9.
MOVE 10000 TO 10K.
SUBTRACT VAR9 FROM 10K GIVING RESULT.
DISPLAY "VARX ", VARX.
DISPLAY "VAR9 ", VAR9.
DISPLAY "10K ", 10K.
DISPLAY "RESULT ", RESULT.
END PROGRAM TEST1.

Here is the result:
VARX 1000 0
VAR9 1000 0
10K 000010000
RESULT 099990000
 
There have been several recent threads in the General Cobol forum on this topic, but I am having trouble finding the one I liked best.

Anyway, the simplest, most elegant solution I've seen follows. If you know there are no leading spaces, try this:

Code:
WORKING-STORAGE SECTION.
  01  VARX                     PIC X(20) VALUE "1000".
  01  VAR9                     PIC 9(9)  VALUE 0.
PROCEDURE DIVISION.
  ...
  UNSTRING VARX DELIMITED BY SPACES INTO VAR9
  ...
"Code what you mean,
and mean what you code!
But by all means post your code!"

Razalas
 
Razalas,

Not only is it elegant, but it works. Thanks for your suggestion.

It's been many years since I used COBOL seriously and then the COBOL I was using didn't have an unstring command. I have some reading to do.

In analyzing this problem I've also discovered that when I accept data into a numeric field it justifies to the left. That was a surprise as well. Can't understand why it would be that way. I have some reading to do to solve that problem.

Thanks to everyone who answered my post. I learned something from each one. I love this forum!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top