Menu
CoddyTech

Dollars and Commas

Part of the Data and Numbers section of Coddy's COBOL Programming journey. Lesson 10 of 23.

A comma in an edited picture prints a comma between digits. When the digits to its left are suppressed, the comma becomes a space too:

01 BIG-OUT PIC Z,ZZ9.
    MOVE 1250 TO BIG-OUT. *> "1,250"
    MOVE 999 TO BIG-OUT. *> "  999"

One $ at the start prints a dollar sign in that place, however many spaces follow it:

01 PRICE-OUT PIC $Z,ZZ9.99.
    MOVE 12.5 TO PRICE-OUT. *> "$   12.50"

Several $ signs make the sign float: it moves right and prints next to the first digit. The first $ only holds the sign, so $$,$$9.99 has room for 4 digits before the point:

01 PRICE-OUT PIC $$,$$9.99.
    MOVE 12.5 TO PRICE-OUT. *> "   $12.50"

* fills leading positions with asterisks, as on a printed check, so nobody can write digits in front of the amount: $***9.99 prints 12.5 as $**12.50.

challenge icon

Challenge

Easy

Declare PRICE-OUT so prices print with a floating dollar sign, a comma and cents: 1234.5 prints Price: $1,234.50 and 7.5 prints Price:     $7.50. Prices go up to 9,999.99.

Example input, one line per ACCEPT: 1234.50.

Try it yourself

       IDENTIFICATION DIVISION.
       PROGRAM-ID. PRICES.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 PRICE PIC 9(5)V99.
       *> Declare PRICE-OUT here
       PROCEDURE DIVISION.
           ACCEPT PRICE.
           MOVE PRICE TO PRICE-OUT.
           DISPLAY "Price: " PRICE-OUT.
           STOP RUN.

All lessons in Data and Numbers