Skip to main content

COBOL Tutorial 00300 – Edited Fields

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

Popular posts from this blog

Load Testing ASP.NET Sites with JMeter

Following my previous post about using JMeter to test MOSS, I tried to figure out what are the bare minimum requirements of using JMeter against a plain ASP.NET website. I wrote a very simple ASP.NET web application with just a button, a text fields and a static label. This application displays the content of a text file in the static label when it loads and write content of the text field back to the file when the button is clicked. I found all I need to do in order to script this using JMeter is to extract __VIEWSTATE and __EVENTVALIDATION fields then send them back in the update request. My JMeter test plain looks like this:

Getting HP Mini 1000 Wireless to Work Under Ubuntu 9.10 Karmic Koala‎ Netbook Remix

I installed Ubuntu 9.10 Netbook Remix in my (actually my wife's) HP mini 1000 this afternoon. To my surprise the wireless card did not work. Also, when I looked at System -> Administration -> Hardware Drivers, the list was blank. After few hours of googling and reading through several not too helpful forum posts, I learned that this was caused by Ubuntu 9.10 shipping "b43" driver out of box, which does not work for HP mini 1000. The proprietary driver "wl" should be used instead. However, no one said exactly what I needed to do to fix this problem. Eventually, I decided to just launch Synaptic and search for "broadcom". The first result in the filtered list was bcmwl-kernel-source, which looked promising so I just went ahead and installed it. I had a look at the /etc/modprobe.d folder after the installation finished, I noticed that the package actually created a blacklist file for "b43" related modules for me already. After reb

Load Testing SharePoint (MOSS) Sites with JMeter

I have used JMeter for load testing few non-ASP.NET web sites before, however I could not get it to work with ASP.NET web sites. This is mainly due to ASP.NET ViewState and event validations, which stops a recorded JMeter script from being played back. Recently I worked on a MOSS project and we were looking for tools to perform load testing on the server. Many people said the load testing tool in Microsoft Team System for Testers works well with MOSS. However, it is quite expensive so I decided to give JMeter another go. After several hours of hacking, I actually got it to work and here’s how I did it. My test page is the pretty standard MOSS edit document property screen with few extra text fields added and the goal here is to use a JMeter script to change the document properties. Once I have a working script, I can configure JMeter to fire hundreds of instances of this script simultaneously to simulate the user workload. As shown in the screenshot below, the test plan contai