As I have mentioned in the last tutorial, you use edited fields in COBOL to format data fields into human-readable display strings. Let’s start with a numeric field:
01 NUMERIC-FIELD PIC 999999V99.
and some COBOL code that set and display the field value:
MOVE 1234.5 TO NUMERIC-FIELD. DISPLAY NUMERIC-FIELD: ' NUMERIC-FIELD.
As we’ve demonstrated in the previous tutorial, unused digits are padded with ugly zeros:
NUMERIC-FIELD: 001234.50
Let me put my C# programmer hat on again (apologies to Java, ruby, python, C/C++, assembly and many other programmers who don’t like C#), when we have to format a variable for display, we often use the string.Format method with a formatting string containing special formatting characters, which is “0,0.00” in the following example:
// returns 1,234.50 string.Format("{0:0,0.00}", 1234.5)
Now let’s come back to COBOL, an edited field is basically a normal COBOL data field with a formatting string in the picture clause instead of the “A”, “X” or “9” data type specifiers. The edit field's formating string is based on similar ideas as C#’s one. To achieve the same output as the C# code above, I use “ZZZ,ZZZ.99” formating string as shown in the following example:
01 EDITED-NUMERIC-FIELD PIC ZZZ,ZZZ.99.
Unlike the place holding character “9”, each unused “Z” in the picture clause is not filled with “0” and the “,” just inserts a comma in the display value. Therefore, if we move the value of NUMERIC-FIELD to the EDITED-NUMERIC-FIELD and then display the content of EDIT-NUMERIC-FIELD:
MOVE NUMERIC-FIELD TO EDITED-NUMERIC-FIELD. DISPLAY 'EDITED-NUMERIC-FIELD: ' EDITED-NUMERIC-FIELD.
The result is a much more readable output:
EDITED-NUMERIC-FIELD: 1,234.50
Keep in mind that an edited field is basically an alpha-numeric field, so you cannot perform arithmetic calculation with it. For example if you add following line of code to your program:
ADD 1 TO EDITED-NUMERIC-FIELD.
You will get the following compile error:
Error: 'EDITED-NUMERIC-FIELD' is not numeric name
This is a summary of commonly used formatting special characters:
- “B” – Inserts a blank space.
- “Z” – Place holder for a numeric character or space if unused.
- “,” – inserts a comma.
- “/” – Inserts a slash.
- “0” – Inserts a zero.
Here are some examples to demonstrate how to use them:
Picture Clause | Input | Output |
9999/99/99 | 20100101 | 2010/01/01 |
9999B99B99 | 20100101 | 2010 01 01 |
-ZZZ,ZZZ.99 | -1234.5 | -1234.50 |
ZZZ,ZZZ.99- | -1234.5 | 1234.50- |
X0X0X0X | ABCD | A0B0C0D |
Finally, here's the COBOL program I used to develop the example code in this tutorial.
IDENTIFICATION DIVISION. PROGRAM-ID. EDITED-FIELD. DATA DIVISION. WORKING-STORAGE SECTION. 01 NUMERIC-FIELD PIC 999999V99. 01 EDITED-NUMERIC-FIELD PIC ZZZ,ZZZ.99. 01 EDITED-NEGATIVE-FIELD1 PIC -ZZZ,ZZZ.99. 01 EDITED-NEGATIVE-FIELD2 PIC ZZZ,ZZZ.99-. 01 EDITED-DATE-FIELD1 PIC 9999/99/99. 01 EDITED-DATE-FIELD2 PIC 9999B99B99. 01 EDITED-ZERO-FIELD PIC X0X0X0X. PROCEDURE DIVISION. MOVE 1234.5 TO NUMERIC-FIELD. DISPLAY ' NUMERIC-FIELD: ' NUMERIC-FIELD. MOVE NUMERIC-FIELD TO EDITED-NUMERIC-FIELD. DISPLAY 'EDITED-NUMERIC-FIELD: ' EDITED-NUMERIC-FIELD. MOVE -1234.5 TO EDITED-NEGATIVE-FIELD1. DISPLAY 'EDITED-NEGATIVE-FIELD1: ' EDITED-NEGATIVE-FIELD1. MOVE -1234.5 TO EDITED-NEGATIVE-FIELD2. DISPLAY 'EDITED-NEGATIVE-FIELD2: ' EDITED-NEGATIVE-FIELD2. MOVE 20100101 TO EDITED-DATE-FIELD1. DISPLAY 'EDITED-DATE-FIELD1: ' EDITED-DATE-FIELD1. MOVE 20100101 TO EDITED-DATE-FIELD2. DISPLAY 'EDITED-DATE-FIELD2: ' EDITED-DATE-FIELD2. MOVE 'ABCD' TO EDITED-ZERO-FIELD. DISPLAY 'EDITED-ZERO-FIELD: ' EDITED-ZERO-FIELD. STOP RUN.
Comments
Post a Comment