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

Converting alphanumeric field to numeric

Status
Not open for further replies.

DJRB

Programmer
Apr 24, 2002
3
0
0
US
Hi,
I've been away from Cobol for a while and need some advice on how to accomplish something. I'm working on an HP3000, MPEXL OS Using HP CobolII. I have an alphanumeric field which is 7 characters long, Pic x(07). However, the values contained in this field represent numeric dollar amounts such as: 100.00
10.00
1.00
.10
.01
The data is always left-justified and the decimal point can appear anywhere in the first 5 digits of the field. I need to convert this value into its correct numeric representation in the format of Pic 9(04)v99, so it can be added to other totals. I'm looking into Unstring and Inspect commands, but am short on time. Any advice would be greatly appreciated. Thanks
 
You will need to test this as I just did it off the top of my head. You may need to add extra validation in case the input is not as you said it was.

IDENTIFICATION DIVISION.
PROGRAM-ID. MNUMERIC.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WORK-AREAS.
05 IN-WORK.
10 IN-WORK-BYTE PIC X OCCURS 07 TIMES
INDEXED BY IN-IDX.
05 OUT-WORK.
10 OUT-WORK-BYTE PIC X OCCURS 07 TIMES
INDEXED BY OUT-IDX.
LINKAGE SECTION.
01 MNUMERIC-PARAMETERS.
05 MNUMERIC-STRING PIC X(07).
05 MNUMERIC-NUMBER PIC 9(04)V99.
PROCEDURE DIVISION USING MNUMERIC-PARAMETERS.
MOVE MNUMERIC-STRING TO IN-WORK
PERFORM
VARYING IN-IDX FROM 7 BY -1
UNTIL IN-IDX LESS THAN 1
OR IN-WORK-BYTE (IN-IDX) NUMERIC
END-PERFORM
SET OUT-IDX TO 6
PERFORM
VARYING IN-IDX FROM IN-IDX BY -1
UNTIL IN-IDX LESS THAN 1
IF IN-WORK-BYTE (IN-IDX) NOT EQUAL "."
MOVE IN-WORK-BYTE (IN-IDX)
TO OUT-WORK-BYTE (OUT-IDX)
SET OUT-IDX DOWN BY 1
END-IF
END-PERFORM
MOVE OUT-WORK TO MNUMERIC-NUMBER
GOBACK.
 
Hi,
This will work per your example where there is always one decimal point and two decimal positions in the data:

77 space-count pic 9.
01 input-field pic x(7).
01 output-field.
02 output-num pic 9(4).
02 filler pic x.
02 output-dec pic v99.
01 new-num pic 9(4)v99.


move 0 to space-count.
move spaces to output-field.
inspect input-field tallying space-count for all space.
if space-count = 1 string " ", input-field delimited by
size into output-field
else if space-count = 2 string " ", input-field
delimited by size into output-field
else if space-count = 3 string " ", input-field
delimited by size into output-field
else if space-count = 4 string " ", input-field
delimited by size into output-field.
inspect output-field replacing all space by zero.
move output-num to new-num.
add output-dec to new-num.
 
I have had a brief look at the HP online documentation. Try redefining the source as numeric edited, then moving the numeric edited item to the numeric item (using a often unnoticed capability of the MOVE statement to deedit).

01 my-alpha-item pic X(07) value "12.34".
01 my-num-edit-item pic Z(07) redefines my-alpha-item.

01 my-num-item pic 9(4)v99.

....

MOVE my-num-edit-item to my-num-item.
display my-num-item.

====

The HP doc does not say how picky HP COBOL II is about using the edit picture in the deedit process, so give this a try using several test values. It may be the easiest way by far to accomplish your requirement.
Tom Morrison
 
Hi Tom,

I don't think your suggestion can work. When you use a numeric edited mask the result is right justified; DJRB's data is left jutified.

If (s)he's using the most current compiler std, I think NUMVAL should work.

Regards, Jack.
 
01 my-alpha-item pic X(07) value "12.34".
01 my-num-edit-item pic Z(07) redefines my-alpha-item.

01 my-num-item pic 9(4)v99

how about
move function numval (my-alpha-item) to my-num-item

 
Of course, this will only work when the values are left justified and contain legitimate numbers, but it's short:

01 DOLLAR-VALUE PIC 9(5)V99.

01 WORK-FIELD PIC Z(5).99.

01 REDEFINES WORK-FIELD.
05 INTEGER-PORTION PIC X(5) JUST RIGHT.
05 DECIMAL-POINT PIC X VALUE ".".
05 DECIMAL-PORTION PIC 99.


MOVE SPACES TO INTEGER-PORTION.
MOVE ZEROS TO DECIMAL-PORTION.
UNSTRING DOLLAR-STRING-FIELD DELIMITED BY "."
INTO INTEGER-PORTION
DECIMAL-PORTION.
MOVE WORK-FIELD TO DOLLAR-VALUE. Betty Scherber
Brainbench MVP for COBOL II
 
You might check to see what intrinsic functions you can use if any. Numvalc might be something to try if your compiler has that capability.
If you do not like my post feel free to point out your opinion or my errors.
 
Hi HRayT,

I don't think MOVE will work w/NUMVAL; COMPUTE's the ticket. Ask John Lovitz. :)

Jack
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top