Dealing with percents in COBOL:

Percents involve an interesting problem. If you want to take a percent of something you need the decimal to use in the calculation and the whole number to print. For example, if you want to take 25% of something you need the decimal .25, but if you want to print the 25% on the printer you want the whole number. There are several solutions to this problem:

Solution 1:

The decimal that you need to use in calculations is defined on the input record if it is a field that comes in on the input record or in more likely as a constant in WORKING-STORAGE. You also need to define the place to print the percent on the detail line either in the FILE SECTION or in WORKING STORAGE.

It could be defined on the input record as:

05  DEC-PER        PIC V99.

or if you know the exact percentages you are using, they could be defined in WORKING-STORAGE. Let's say in some circumstances you want to use 15% and in some cases you want to use 25%. You could set up a constant for each and store the decimal version, .15 and .25:

01  CONSTANTZ.
05  FIRST-PER-WS       PIC V99    VALUE .15.
05  SECOND-PER-WS      PIC V99    VALUE .25.

If you define your print line in the WORKING-STORAGE SECTION, the area to print the percentage might look like this.

01  DETAIL-LINE.
...
05  PERCNT-PR                   PIC 99     VALUE 0.
05  PERCNT-SGN-PR               PIC X      VALUE "%"

Remember, if you define a line in the WORKING-STORAGE SECTION, when it is time to write it you must say something like WRITE PRINTZ FROM DETAIL-LINE where PRINTZ is defined asthe 01 level of the FD. The reason for this is, all input and output has to go through the file section. You cannot a line set-up in the WORKING-STORAGE SECTION directly.

If you define your print line in the FILE SECTION you cannot use the VALUE clause so the line would look like this. (Remember, the only VALUE allowed in the FILE SECTION is a level 88 value.)

01  PRINTZ.
...
05  PERCNT-PR                     PIC 99.
05  PERCNT-SGN-PR                 PIC X.

In the PROCEDURE DIVSION:

In the routine where you are calculating and setting up the line, you would use the following code to do the calculation if the percent came in on the input:

MULTIPLY AMT-WS BY DEC-PER
GIVING ANS-WS.

If the decimal was stored in a constant in WORKING-STORAGE, you must determine when to use FIRST-PER-WS and when to use SECOND-PER-WS. I decided that if a field on the input called CODE was equal to A then I would use FIRST-PER-WS otherwise I would use SECOND-PER-WS, therefore the code would be:

IF CODEZ = "A"
MULTIPLY AMT-WS BY FIRST-PER-WS
GIVING ANS-WS
ELSE
MULTIPLY AMT-WS BY SECOND-PER-WS
GIVING ANS-WS.

Then when you are ready to print the line. you would do the following if the decimal came in on the input:

MULTIPLY DEC-PER BY 100
GIVING PERCNT-PR.

If the decimal was a constant in WORKING-STORAGE, then the code would be:

IF CODEZ = "A"
MULTIPLY FIRST-PER-WS BY 100
GIVING PERCNT-PR
ELSE
MULTIPLY SECOND-PER-WS BY 100
GIVING PERCNT-PR.
If the line was set up in WORKING STORAGE with the % in the VALUE clause, then the percent sign is already on the line (just be careful that you don't MOVE SPACES TO DETAIL-LINE or you will wipe it out). If the line was set up in the FILE SECTION you can't have VALUE clauses, so the % is not there and must be moved. The way you do this is to include this move when you are setting up the line:

MOVE "%" to PERCNT-SGN-PR.

Solution 2:

This is an approach that takes advantage of the V which is an assumed decimal point but not an actual printing decimal point.

On the print line (whether it is set up in the FILE SECTION or the WORKING-STORAGE SECTION), you should define PERCNT-PR with a PICTURE of V99.

05  PERCNT-PR          PIC  V99.

Now, you do not have to multiply the percent by 100 giving the place on the printline, you can simply move it. If the decimal was .25 the decimal point and the V will line up so the number will move in correctly, but because of the V there will be no actual decimal point on the line and only the 25 will print. The code to move the percent if it came in on the input record would be:

MOVE DEC-PER TO PERCNT-PR.

If the percent was defined as a constant in WORKING-STORAGE the moves would be embedded in an IF and look like this:

IF CODEZ = "A"
MOVE FIRST-PER-WS TO PERCNT-PR
ELSE
MOVE SECOND-PER-WS TO PERCNT-PR.

Again, if the line is defined in the FILE SECTION, you need the code to move the % to the line:

MOVE "%" TO PERCNT-SGN-PR.