如何在 COBOL 中将整数值转换为小数值
如何在 COBOL 中将整数值转换为小数值?
我需要将 1234567 显示为 12345.67
我无法将该变量除以 100。
How do I convert an integer value to decimal value in COBOL?
I need to display 1234567 as 12345.67
I cannot divide the variable by 100.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
COBOL 提供了多种数字表示形式。有些有利于
算术运算,其他便于演示。它需要
花点时间把它们全部整理出来。
我认为您可能正在寻找隐含的十进制表示形式。隐含的
十进制在 PICture 字符串中由字母“V”表示,例如:
上述两个声明都包含 7 位数字,占用 7 个字节的存储空间。
您可以分配
值 1234567 到 WS-INT-NBR 如下:
但对 WS-DEC-NBR 执行相同操作将
导致溢出(截断),因为它只能容纳之前的5位数字
隐含的小数点(“V”)。但是,如果您要执行以下操作:
WS-DEC-NBR 的实际内容将是
1234567
(注意小数点消失了,它包含一个相当于 WS-INT-NBR 的值)。这就是隐含的意思
小数点。 COBOL“知道”WS-DEC-NBR 在
第 5 和第 6 位数字但实际上并不存储它。应用的任何操作
WS-DEC-NBR 将考虑隐含的小数点。
有了这些知识,您就可以使用另一个 COBOL 功能,即
重新定义声明。 REDEFINES 告诉编译器应用不同的
数据类型处理规则到同一存储区域。在一种情况下你想要
将内存区域视为整数:PIC 9(7);在其他情况下,例如隐含的小数
值:PIC 9(5)V99)。
执行以下操作:
现在 WS-INT-NBR 和 WS-DEC-NBR 占用相同的内存区域(相同的 7 个字节)。然而
当你引用
WS-INT-NBR 使用整数表示。当您引用 WS-DEC-NBR 时,小数点
使用表示法。
这让我们解决了问题中“不能除以 100”的问题。下一位是
需要时显示小数点。 COBOL 提供的 DISPLAY 格式包含
明确的“标点符号”,其中小数点为一位。例如:
WS-DEC-DISPLAY 在其 PICture 子句中包含显式小数点(不要
将行末尾的句点与嵌入的 PICture 子句小数点混淆)。
WS-DEC-DISPLAY 的内容包含明确的小数点。所以,要得到小数
要显示,您需要将带有隐含小数点的内容移动到其中,如下所示:
如果 WS-DEC-NBR 包含“1234567”,则 WS-DEC-DISPLAY 将在 MOVE 之后包含“12345.67”。
以下程序和显示为您整理了所有内容:
输出:
COBOL provides several numeric representations. Some facilitate
arithmetic operations, others facilitate presentation. It takes
a while to sort them all out.
I think you may be looking for an implied decimal representation. The implied
decimal is indicated by the letter 'V' in a PICture string for example:
Both of the above declarations contain 7 digits and occupy 7 bytes of storage.
You can assign the
value 1234567 to WS-INT-NBR as follows:
but doing the same to WS-DEC-NBR would
cause an overflow (truncation) because it can only hold 5 digits before
an implied decimal point (the 'V'). However if you were to do the following:
the actual contents of WS-DEC-NBR would be
1234567
(notice the decimal pointis gone and it contains a value equivalent to WS-INT-NBR). That is what is meant by implied
decimal point. COBOL 'knows' that WS-DEC-NBR has a decimal point between the
5th and 6th digits but does not actually store it. Any operations applied
to WS-DEC-NBR will take the implied decimal point into consideration.
Armed with this knowledge you can then make use of another COBOL feature, the
REDEFINES declaration. REDEFINES tells the compiler to apply different
data type processing rules to the same storage area. In one case you want to
treat the memory area like an integer: PIC 9(7); and in other cases like an implied decimal
value: PIC 9(5)V99).
Do this as follows:
Now WS-INT-NBR and WS-DEC-NBR occupy the same memory area (the same 7 bytes). However
when you reference
WS-INT-NBR the integer representation is used. When you reference WS-DEC-NBR the decimal
representation is used.
That gets us over the 'not dividing by 100' part of your problem. The next bit is
displaying the decimal point when you need to. COBOL provides DISPLAY formats containing
explicit 'punctuation' of which the decimal point is one. For example:
WS-DEC-DISPLAY contains an explicit decimal point within its PICture clause (do not
confuse the period at the end of the line with the imbedded PICture clause decimal point).
The content of WS-DEC-DISPLAY contains an explicit decimal point. So, to get the decimal
point to display you need to MOVE something with an implied decimal point to it, as in:
if WS-DEC-NBR contained '1234567', WS-DEC-DISPLAY will contain '12345.67' after the MOVE.
The following program and displays put it all together for you:
Output:
使用隐含的小数重新定义您的值。 EG
当您将其称为 ws-decimal 时,您将得到 12345.67。
因此,如果将某些内容移动到 ws-integer,您可以使用其他字段在正确位置使用小数来访问它。
Redefine your value using an implied decimal. E.G.
When you refer to this as ws-decimal, you will get 12345.67.
So if you move something to ws-integer, you can access it with the decimals in the correct position using the other field.