如何在 COBOL 中将字母数字字符串转换为十进制数字

发布于 2024-08-27 01:20:10 字数 75 浏览 11 评论 0原文

例如,我有字母数字字符串“ABCDEF 0 0.450”,我需要将“0.450”作为数字十进制并对其进行算术运算。我们有办法吗?请建议。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眸中客 2024-09-03 01:20:10

我假设格式不固定,从那时起,您可以选择字符串的该部分并将其移动到由 pic 9.999 定义的字段并从那里使用它。

这假设字符串值由 3 个部分组成,并用空格分隔。首先定义一些字段:

 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).

An 后缀来自我的命名约定,表示字母数字值。如果字符串输入可能更长,请根据需要增加大小。

代码的其余部分将 theString 解析为 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

请注意,我还没有通过编译器运行它!

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!

宫墨修音 2024-09-03 01:20:10

通常,我可以找到实现它的方法!

如上所述,UNSTRING 和组合不起作用,但 REDEFINES 起作用!

将字母数字字符串重新定义为两个数字字段,以分别保存和处理小数部分和整数部分。最后将小数部分除以 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。希望你能算出来。

并请建议任何其他方法来实现相同的目标!

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!

云胡 2024-09-03 01:20:10

在大多数现代 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 进行计算。

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.

赏烟花じ飞满天 2024-09-03 01:20:10

例如,我有字母数字字符串
'ABCDEF 0 0.450' 我需要得到
'0.450' 作为小数并做
关于它的算术。我们有办法吗?
请提出建议。

如果您在 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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文