English 中文(简体)
How to convert a alphanumeric string into numeric decimal in COBOL
原标题:

For eg., i have alphanumeric string ABCDEF 0 0.450 and i need to get 0.450 as numeric decimal and do arithmetic on it. Do we have a way? Please suggest.

最佳回答

Asusually, I could find out a way to achieve it!

As said above, UNSTRINGing and combining didnt work, but REDEFINES works!

Get alphanumeric string redefined into two numeric fields to hold and process decimal part and integer part individually. Atlast divide the decimal-total by 1000 and add it to integer-part. Thats it! Code snippets follow...

01 WS-NUM-STR      PIC X(14) VALUE  ABCDEF 0 0.450 .
01 WS-NUM-STR-MOD  REDEFINES WS-NUM-STR.
   05 FILLER       PIC X(9).
   05 WS-INT-PART  PIC 9.
   05 FILLER       PIC X.
   05 WS-DEC-PART  PIC 999.

----------
----------

*  CODE TO ADD ALL INTEGER PARTS
*  CODE TO ADD ALL DECIMAL PARTS
   COMPUTE = INTEGER-TOTAL + (DECIMAL-TOTAL / 1000)

Note: As I already knew that decimal part has 3 digits, I divided DECIMAL-TOTAL by 1000. Hope you could figure out.

And Please suggest any other way to achieve the same!

问题回答

I assume the format is not fixed, since then you could just pick that part of the string and move it to a field defined with pic 9.999 and use it from there.

This assumes the string value is made up of 3 parts separated by spaces. First define some fields:

 1   part1       pic  x(14).
 1   part2       pic  x(14).
 1   part3       pic  x(07).

 1   digitsAn.
     2 digits    pic  9(03).
 1   decimalsAn.
     2 decimals  pic .9(03).

 1   theValue    pic  9(03)v9(3).

The An suffix is from my naming convention, and indicates a alphanumeric value. If the string input may be longer increase the sizes as needed.

The rest of the code parses theString into theValue.

*    Extract the third part.
     initialize part3
     unstring theString delimited by all spaces into part1 part2 part3

*    make sure the receiving field contains numeric data.
     inspect part3 replacing spaces by zeroes

*    extract digits before and after the decimal point.
     unstring part3 delimited by "." into digits decimalsAn(2:)

*    Combine parts before and after decimal points into a numeric value.
     compute theValue = digits + decimals

Beware, I haven t run this through a compiler!

In most modern Cobol compilers, you can move a numeric-edited field to a numeric field.

Borrowing from your example below:

01 WS-NUM-STR           PIC X(14) VALUE  ABCDEF 0 0.450 .
01 WS-NUM-STR-MOD       REDEFINES WS-NUM-STR.
   05 FILLER            PIC X(9).
   05 WS-EDITED-NUMBER  PIC 9.999.
01 WS-NUMBER            PIC 9V999.

----------

MOVE WS-EDITED-NUMBER TO WS-NUMBER

And then, do the calculation with WS-NUMBER.

For eg., i have alphanumeric string ABCDEF 0 0.450 and i need to get 0.450 as numeric decimal and do arithmetic on it. Do we have a way? Please suggest.

If you are running in CICS, you could use the BIF DEEDIT function -- that will leave you with "00.450".

If you are not running in CICS, assume you have your string in a field called WS-STR:

Move 0 to JJ
Perform varying II from 1 by 1 
 until II > Length of WS-STR 

 Evaluate WS-STR (II:1)
   when  0 
   when  1 
   when  2 
   when  3 
   when  4 
   when  5 
   when  6 
   when  7 
   when  8 
   when  9 
   when  . 
     Add 1 to JJ
     Move WS-STR (II:1) to WS-NUM (JJ:1)

* ---- Do nothing
   when other
     continue

End-Perform
Compute REAL-DEC-NUM = Function NUM-VAL (WS-NUM)




相关问题
architectures of COBOL and Java

Can anyone tell me the differnces in styles and architectures between these very differenct approaches please?

How to decrypt a string in cobol

i m looking for ways to decrypt a string in cobol that was encrypted in .net using RijndaelManaged. I have control of the algorithm so it doesn t need to be Rijdnael, but i just need to ensure that ...

How can duplicates be removed from a file using COBOL?

The input file have records as: 8712351,8712353,8712353,8712354,8712356,8712352,8712355 8712352,8712355 Using COBOL, I need to remove duplicates from the above file and write to an output file. I ...

热门标签