たとえば、英数字の文字列'ABCDEF 0 0.450'があり、数値の小数として' 0.450'を取得し、それに対して算術演算を行う必要があります。方法はありますか?提案してください。
4 に答える
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!
最近のほとんどの COBOL コンパイラでは、数値編集フィールドを数値フィールドに移動できます。
以下の例から借ります:
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
そして、WS-NUMBER で計算します。
いつものように、私はそれを達成する方法を見つけることができました!
上記のように、UNSTRING と結合は機能しませんでしたが、REDEFINES は機能します!
小数部分と整数部分を個別に保持および処理するために、2 つの数値フィールドに再定義された英数字文字列を取得します。最後に、10 進数の合計を 1000 で割り、整数部分に追加します。それでおしまい!コードスニペットが続きます...
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)
注: 小数部分が 3 桁であることは既にわかっていたので、DECIMAL-TOTAL を 1000 で割りました。
そして、同じことを達成するための他の方法を提案してください!
たとえば、英数字文字列 'ABCDEF 0 0.450' があり、'0.450' を 10 進数として取得し、それに対して演算を行う必要があります。方法はありますか?提案してください。
CICS で実行している場合は、BIF DEEDIT 関数を使用できます。これにより、「00.450」が残ります。
CICS で実行していない場合は、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)