3

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,

Enter image description here

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.
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123

1 Answers1

9

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.

cschneid
  • 9,037
  • 1
  • 28
  • 34