Highlighted
Absent Member.
Absent Member.
2699 views

Display wrong format in micro focus visual cobol.

Jump to solution

Hi.

Recently, i code a test program as below :

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-TEST PIC 9(5)V9(2) VALUE 13.5.
01 WS-NUM3 PIC S9(3)V9(2) VALUE -123.45.

PROCEDURE DIVISION.
DISPLAY WS-TEST.
DISPLAY WS-NUM3.
STOP RUN.

When i run by ctrl + f11 then the result in console command is :

0001350
12345-

But when i run this project on OpenCobolIDE then result is correct : 

00013.50

-123.45

Plz help me.

Tags (1)
0 Likes
1 Solution

Accepted Solutions
Highlighted
Absent Member.
Absent Member.

RE: Display wrong format in micro focus visual cobol.

Jump to solution

You can do the following:

      01  WS-TEST   pic 9(5)v9(2) value 13.5.

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

      01  WS-TEST-2 pic 9(5).9(2) value 13.5.

      01  WS-NUM3-2 pic ---9.9(2) VALUE -123.45.

          display WS-TEST.

          display WS-NUM3.

          display WS-TEST-2

          display WS-NUM3-2.

Results would be:

0001350  

12345-  

00013.50

-123.45  

Or you can write:

      01  WS-TEST   pic 9(5)v9(2) value 13.5.

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

      01  WS-TEST-2 pic 9(5).9(2).

      01  WS-NUM3-2 pic ---9.9(2).

          move ws-test to ws-test-2.

          move ws-num3 to ws-num3-2.

          display ws-test-2.

          display ws-num3-2.

Results would be:

00013.50

-123.45  

View solution in original post

0 Likes
5 Replies
Highlighted
Absent Member.
Absent Member.

RE: Display wrong format in micro focus visual cobol.

Jump to solution

You can do the following:

      01  WS-TEST   pic 9(5)v9(2) value 13.5.

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

      01  WS-TEST-2 pic 9(5).9(2) value 13.5.

      01  WS-NUM3-2 pic ---9.9(2) VALUE -123.45.

          display WS-TEST.

          display WS-NUM3.

          display WS-TEST-2

          display WS-NUM3-2.

Results would be:

0001350  

12345-  

00013.50

-123.45  

Or you can write:

      01  WS-TEST   pic 9(5)v9(2) value 13.5.

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

      01  WS-TEST-2 pic 9(5).9(2).

      01  WS-NUM3-2 pic ---9.9(2).

          move ws-test to ws-test-2.

          move ws-num3 to ws-num3-2.

          display ws-test-2.

          display ws-num3-2.

Results would be:

00013.50

-123.45  

View solution in original post

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Display wrong format in micro focus visual cobol.

Jump to solution

Thanks Ferda.Tanyeri.

But could you show me the reason ?

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Display wrong format in micro focus visual cobol.

Jump to solution

The reason is simple: Any numeric variable with a virtual decimal sign "v" is for calculations only. The data is stored without any decimal point position. So the value 12345 is stored just like that. The actual value depends on the programmer who determines the position of decimal point. You can define a variable as 999v99 in one program and as 99v999 in another program. When you read the data from a file in the first case it will be 123.45 in the second 12.345. THE DECIMAL POINT POSITION IS DYNAMIC AND FOR CALCULATION ONLY.

However declarations such as 9(3).9(2) or ----.99 are formatted and for display purposes. Here is the decimal point position STATIC, i.e. it is fixed.

I do strongly believe that back in 1950's and 1960's, when the cost of storage devices were extremely high, COBOL developers wanted to save every byte possible. With dynamic decimal point positioning, on every decimal value they saved one Byte. I think thats the reason why it is working this odd way.

0 Likes
Highlighted
Outstanding Contributor.
Outstanding Contributor.

RE: Display wrong format in micro focus visual cobol.

Jump to solution

I think Cobol should do an intelligent conversion to the display format with setting a decimal point at the correct position. Defining the variable with comp-3 gives the same result. Display of a variable defined as decimal gives a correct result. But for the moment you cannot use Cobol arithmetic verbs with decimal; you can use the Set command. I have an open support case for that.

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Display wrong format in micro focus visual cobol.

Jump to solution

"I think Cobol should do an intelligent conversion" this may require a time machine to change the way standard COBOL operates 🙂  And to answer a poster from above, yes, COBOL does it like this to save every byte as storage was very very expensive way back then.

The DISPLAY verb merely dumps the data-item to the SYSOUT without and regards for data-type.  It will quite happily display the bytes from a BINARY (COMP) or PACKED-DECIMAL (COMP-3) data item.

When handling numeric data in COBOL you always need to format numerics before any output whether with DISPLAY to SYSOUT, to a screen, a report file, an XML file, a CSV file, etc, etc.

A data item defined as PIC S9(5)V99 takes up 7-bytes of storage with an implied decimal point and the sign stored in the trailing high-order half bytes unless defined with a SIGN SEPARATE clause.  E.g. -0.01 would be ASCII x30303030303071 (IIRC) and would display as such.  To get it to display formatted move it to a formatted data item first e.g. PIC -(4)9.99.

Whilst a PIC S9(5)V99 does have an implied USAGE DISPLAY clause it should not be taken literally.

Also, it should be noted that usage display data item shouldn't really be used in mathematical operations or as subscripts.  COBOL will quite happily let you do it but, to use a term from other languages, you will incur a type conversion upon each use of the data item (again this is from the old days when processor speed was at a premium and any assembler instructions you could save were worth it - but the principle still applies).  So the above data item would be better stored as PIC S9(5)V99 USAGE PACKED-DECIMAL which would be 4-bytes.  E.g. -0.01 would be ASCII x0000001D and should be moved to a formatted data item before output.

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.