如何在 COBOL 中将字母数字字符串转换为十进制数字
例如,我有字母数字字符串“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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我假设格式不固定,从那时起,您可以选择字符串的该部分并将其移动到由 pic 9.999 定义的字段并从那里使用它。
这假设字符串值由 3 个部分组成,并用空格分隔。首先定义一些字段:
An 后缀来自我的命名约定,表示字母数字值。如果字符串输入可能更长,请根据需要增加大小。
代码的其余部分将 theString 解析为 theValue。
请注意,我还没有通过编译器运行它!
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:
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.
Beware, I haven't run this through a compiler!
通常,我可以找到实现它的方法!
如上所述,UNSTRING 和组合不起作用,但 REDEFINES 起作用!
将字母数字字符串重新定义为两个数字字段,以分别保存和处理小数部分和整数部分。最后将小数部分除以 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...
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!
在大多数现代 Cobol 编译器中,您可以将数字编辑字段移至数字字段。
借用下面的例子:
然后,用 WS-NUMBER 进行计算。
In most modern Cobol compilers, you can move a numeric-edited field to a numeric field.
Borrowing from your example below:
And then, do the calculation with WS-NUMBER.
如果您在 CICS 中运行,则可以使用 BIF DEEDIT 函数——这将为您留下“00.450”。
如果您没有在 CICS 中运行,则假设您的字符串位于名为 WS-STR 的字段中:
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: