如何在 COBOL 中将整数值转换为小数值

发布于 2024-12-16 11:19:57 字数 86 浏览 2 评论 0原文

如何在 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 技术交流群。

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

发布评论

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

评论(2

我纯我任性 2024-12-23 11:19:57

COBOL 提供了多种数字表示形式。有些有利于
算术运算,其他便于演示。它需要
花点时间把它们全部整理出来。

我认为您可能正在寻找隐含的十进制表示形式。隐含的
十进制在 PICture 字符串中由字母“V”表示,例如:

  WS-INT-NBR       PIC 9(7).
  WS-DEC-NBR       PIC 9(5)V99.

上述两个声明都包含 7 位数字,占用 7 个字节的存储空间。
您可以分配
值 1234567 到 WS-INT-NBR 如下:

  MOVE 1234567 TO WS-INT-NBR

但对 WS-DEC-NBR 执行相同操作将
导致溢出(截断),因为它只能容纳之前的5位数字
隐含的小数点(“V”)。但是,如果您要执行以下操作:

 MOVE 12345.67 TO WS-DEC-NBR

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)。
执行以下操作:

 01.
     02 WS-INT-NBR   PIC 9(7).
     02 WS-DEC-NBR REDEFINES WS-INT-NBR PIC 9(5)V99.

现在 WS-INT-NBR 和 WS-DEC-NBR 占用相同的内存区域(相同的 7 个字节)。然而
当你引用
WS-INT-NBR 使用整数表示。当您引用 WS-DEC-NBR 时,小数点
使用表示法。

这让我们解决了问题中“不能除以 100”的问题。下一位是
需要时显示小数点。 COBOL 提供的 DISPLAY 格式包含
明确的“标点符号”,其中小数点为一位。例如:

 01  WS-DEC-DISPLAY    PIC 9(5).99.

WS-DEC-DISPLAY 在其 PICture 子句中包含显式小数点(不要
将行末尾的句点与嵌入的 PICture 子句小数点混淆)。
WS-DEC-DISPLAY 的内容包含明确的小数点。所以,要得到小数
要显示,您需要将带有隐含小数点的内容移动到其中,如下所示:

 MOVE WS-DEC-NBR TO WS-DEC-DISPLAY

如果 WS-DEC-NBR 包含“1234567”,则 WS-DEC-DISPLAY 将在 MOVE 之后包含“12345.67”。

以下程序和显示为您整理了所有内容:

   IDENTIFICATION DIVISION.                            
   PROGRAM-ID. EXAMPLE.                                
   DATA DIVISION.                                      
   WORKING-STORAGE SECTION.                            
   01.                                                 
       02 WS-INT-NBR   PIC 9(7).                         
       02 WS-DEC-NBR REDEFINES WS-INT-NBR PIC 9(5)V99. 

   01  WS-DEC-DISPLAY    PIC 9(5).99.

   PROCEDURE DIVISION.                                 
       MOVE 1234567 TO WS-INT-NBR                      
       MOVE WS-DEC-NBR TO WS-DEC-DISPLAY               
       DISPLAY 'WS-INT-NBR    : ' WS-INT-NBR           
       DISPLAY 'WS-DEC-NBR    : ' WS-DEC-NBR           
       DISPLAY 'WS-DEC-DISPLAY: ' WS-DEC-DISPLAY       

       ADD +1 TO WS-INT-NBR                            
       MOVE WS-DEC-NBR TO WS-DEC-DISPLAY               
       DISPLAY 'INT-NBR PLUS 1: ' WS-DEC-DISPLAY       

       ADD +1 TO WS-DEC-NBR
       MOVE WS-DEC-NBR TO WS-DEC-DISPLAY        
       DISPLAY 'DEC-NBR PLUS 1: ' WS-DEC-DISPLAY

       GOBACK                                   
       .                                        

输出:

WS-INT-NBR    : 1234567 
WS-DEC-NBR    : 1234567 
WS-DEC-DISPLAY: 12345.67
INT-NBR PLUS 1: 12345.68   <= notice which digit incremented
DEC-NBR PLUS 1: 12346.68   <= notice which digit incremented

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:

  WS-INT-NBR       PIC 9(7).
  WS-DEC-NBR       PIC 9(5)V99.

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:

  MOVE 1234567 TO WS-INT-NBR

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:

 MOVE 12345.67 TO WS-DEC-NBR

the actual contents of WS-DEC-NBR would be 1234567 (notice the decimal point
is 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:

 01.
     02 WS-INT-NBR   PIC 9(7).
     02 WS-DEC-NBR REDEFINES WS-INT-NBR PIC 9(5)V99.

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:

 01  WS-DEC-DISPLAY    PIC 9(5).99.

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:

 MOVE WS-DEC-NBR TO WS-DEC-DISPLAY

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:

   IDENTIFICATION DIVISION.                            
   PROGRAM-ID. EXAMPLE.                                
   DATA DIVISION.                                      
   WORKING-STORAGE SECTION.                            
   01.                                                 
       02 WS-INT-NBR   PIC 9(7).                         
       02 WS-DEC-NBR REDEFINES WS-INT-NBR PIC 9(5)V99. 

   01  WS-DEC-DISPLAY    PIC 9(5).99.

   PROCEDURE DIVISION.                                 
       MOVE 1234567 TO WS-INT-NBR                      
       MOVE WS-DEC-NBR TO WS-DEC-DISPLAY               
       DISPLAY 'WS-INT-NBR    : ' WS-INT-NBR           
       DISPLAY 'WS-DEC-NBR    : ' WS-DEC-NBR           
       DISPLAY 'WS-DEC-DISPLAY: ' WS-DEC-DISPLAY       

       ADD +1 TO WS-INT-NBR                            
       MOVE WS-DEC-NBR TO WS-DEC-DISPLAY               
       DISPLAY 'INT-NBR PLUS 1: ' WS-DEC-DISPLAY       

       ADD +1 TO WS-DEC-NBR
       MOVE WS-DEC-NBR TO WS-DEC-DISPLAY        
       DISPLAY 'DEC-NBR PLUS 1: ' WS-DEC-DISPLAY

       GOBACK                                   
       .                                        

Output:

WS-INT-NBR    : 1234567 
WS-DEC-NBR    : 1234567 
WS-DEC-DISPLAY: 12345.67
INT-NBR PLUS 1: 12345.68   <= notice which digit incremented
DEC-NBR PLUS 1: 12346.68   <= notice which digit incremented
差↓一点笑了 2024-12-23 11:19:57

使用隐含的小数重新定义您的值。 EG

10 ws-integer                      pic 9(7) value 1234567.
10 ws-decimal redefines ws-integer pic 9(5)v99.

当您将其称为 ws-decimal 时,您将得到 12345.67。

因此,如果将某些内容移动到 ws-integer,您可以使用其他字段在正确位置使用小数来访问它。

Redefine your value using an implied decimal. E.G.

10 ws-integer                      pic 9(7) value 1234567.
10 ws-decimal redefines ws-integer pic 9(5)v99.

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.

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