Decimals in COBOL

给你一囗甜甜゛ 提交于 2020-06-15 06:25:10

问题


I'm very new to COBOL. I'm following the tutorials that came with Micro Focus and I can't seem to get the example to work right. I'm trying to print -123.45 and I keep getting the following,

I looked up a number of posts on here and they don't address my problem. I'm using Micro Focus' Visual COBOL in Eclipse. Here's my code,

   program-id. tictac as "tictac".

   environment division.
   configuration section.

   data division.
   working-storage section.

    01 WS-NUM3 PIC S9(3)V9(2) VALUE -123.45.


   procedure division.
       Display WS-NUM3.
       goback.

   end program tictac.

回答1:


The V in your picture clause is an implied decimal point. I think you want a field with PIC -999.99, where the . is an explicit decimal point, for its picture clause.

You might think of this as COBOL making a distinction between how a variable is defined and how it is shown. Sort of like a format string in printf is just specifying how to show a variable, not how it is defined. Though in both cases the definition and how it is shown have to match up to a certain extent.

Choosing the right picture clause for a numeric field is important; if you're doing calculations it can have a significant performance impact.

So it's common to have a field with a definition such as...

PIC 9(4)V99 COMP-3

...and a corresponding field for output purposes such as...

PIC ZZZ9.99

...so that computations can be done on the first, and when displaying the field is necessary one uses a MOVE statement to copy the contents of the first to the second.



来源:https://stackoverflow.com/questions/61176313/decimals-in-cobol

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!