Moving and Editing Data
Care must be taken when moving (using the MOVE verb) data
to an item. In other languages, such as Pascal, when to assign a value to XYZ
(defined at the beginning of the program as an integer for example), then that's
it. In COBOL, you have to be sure that the value you are moving to XYZ item is
not bigger than the defined size.
A D V E R T I S E M E N T
If you moved 1234 to a PIC 999 defined item,
then if you were to examine XYZ it would contain 234. For numeric data, the
digits are truncated from the left. If item ABC were defined as PIC XXX and you
moved "abcd" into it, on examination you would find it contained "abc". For
alpha-numeric data characters are truncated from the right.
Conversely, moving data that is smaller than the PIC
definition has certain effects. If 2 was moved to item XYZ above, then the
number 2 is written to the right-most position and the leading positions are
zero-filled (see figure below). Likewise, moving "A" to ABC above, the letter
"A" would be written to the left-most position with the trialing positions being
space-filled.
So what about data with decimal places?
To use decimal places, the letter 'V' is used in the PIC description:
01 COST-OF-ITEM PIC 9(4)V99.
Here the item COST-OF-ITEM can contain a number that has two decimal places. 'V'
is called an implied decimal place in that the 'V' itself is not an area
of memory, i.e. the above PIC description will hold 6 digits - 4 before the
decimal point and two after. The computer will align the number being moved into
the item around the decimal point. See the examples below:
number going into COST-OF-ITEM contents of COST-OF-ITEM [PIC 9(4)V99]
1234.56 1234.56
1234 1234.00 (zero-filled)
1 0001.00 (zero-filled)
0.1 0000.10 (zero-filled)
654321.12 4321.12 (digits are truncated)
654321 4321.00 (digits are truncated)
1234.567 1234.56 (figure is NOT rounded up to 1234.57)
If you were to display COST-OF-ITEM it would appear as
123456 since the decimal point is assumed, not actual. For the purposes of
printing or displaying a number you would need to actually show where decimal
point is. You may also wish to avoid having a long string of zeros in front of a
number and have spaces used instead. So the number would first have to be moved
into an outputing item...well have a look at the small program below (don't
worry too much about any of the commands used in the procedure division,
although they're pretty self explanatory):
|