Book Read Free

Michael Coughlan

Page 25

by Beginning COBOL for Programmers-Apress (2014) (pdf)


  had a decimal point in the rightmost position.

  The zero characters are inserted, so there is

  only room in the data for four of the sending

  field’s digits. After decimal-point alignment,

  digits 765 are truncated.

  Size = 6 characters.

  PIC 9(4)V999 7654329

  PIC 999.009

  654.003

  The assumed decimal point aligns with the actual

  decimal point in the receiving field, causing the

  most significant digit (7) to be truncated. The

  zero characters are inserted after the decimal

  point, which only leaves room for one digit; so

  the digits 29 are truncated on the left.

  Size= 7 characters.

  186

  Chapter 9 ■ edited piCtures

  Special-Insertion Editing

  The only special-insertion symbol is the decimal point. The decimal-point insertion symbol has the effect of inserting an actual decimal point into the edited item. This type of editing is called special insertion because of the effect on data moved into the edited item. Data sent to the edited field is aligned along the decimal point, with zero-filling or truncation as required. The decimal point is inserted in the character position where the symbol occurs, and there may be only one decimal point. The decimal-point symbol cannot be mixed with either the V (assumed decimal point) or the P (scaling position) symbol. The purpose and operation of the P symbol is explored later in this chapter.

  Special-Insertion Examples

  Table 9-4 gives some special-insertion example picture strings and shows the formatting that these edited pictures apply to data values. You probably noticed that the last example in Table 9-3 was an example of special insertion as well as simple insertion.

  Table 9-4. Special-Insertion Editing Examples

  Sending

  Receiving

  Comments

  Picture

  Data

  Picture

  Result

  PIC 9(3)V99

  63485 PIC 9999.99

  0634.85

  The decimal point is inserted; and after

  alignment, the digits of the sending item are

  inserted to the left and right of the decimal

  point, with the result that there is zero-filling

  on the left.

  Size = 7 characters.

  PIC 9(4)V99

  063485 PIC 9999.9

  0634.8

  The decimal point is inserted; and after

  alignment, the digits of the sending item are

  inserted to the left and right of the decimal

  point, with the result that there is truncation on

  the right (5).

  Size = 6 characters.

  PIC 9(4)V99

  363485 PIC 999.99

  634.85

  The decimal point is inserted; and after

  alignment, the digits of the sending item are

  inserted to the left and right of the decimal

  point with the result that there is truncation on

  the left (3).

  Size = 6 characters.

  PIC 9(4)

  3485 PIC 999.99

  485. 00

  The decimal point is inserted. The sending

  field is treated as if it had a decimal point in

  the rightmost position. After alignment, there

  is truncation of the leftmost digit (3) and

  zero-filling on the left.

  Size = 6 characters.

  187

  Chapter 9 ■ edited piCtures

  Fixed-Insertion Editing

  Fixed-insertion editing is so named because it inserts the edit symbol in a fixed position at the beginning or end of the edited item. The fixed insertion editing symbols are as follows:

  • The plus (+) and minus (-) signs

  • The letters CR and DB, representing credit and debit

  • The currency symbol (usually the $ sign)

  Like the other insertion edit symbols, the fixed-insertion symbols count toward the size of the printed item.

  Plus and Minus Symbols

  The plus (+) and minus (-) symbols must be placed in the first or last character position of the PICTURE string. The operation of the plus and minus edit symbols is not as straightforward as it may appear. The rules governing their operation are as follows:

  • If the plus symbol is specified, then a minus sign is inserted if the value is negative and a plus

  sign is inserted if the value is positive.

  • If the minus symbol is specified, then a minus sign is inserted if the value is negative but a

  space is inserted if the value is positive. So the minus symbol is only used to highlight negative values. If you always want the appropriate sign to be inserted, use the plus symbol.

  CR and DB

  CR and DB stand for credit and debit, respectively. But what a credit is and what a debit is depends on which side of the balance sheet you are on. Therefore, the rule with the CR and DB symbols is that both are inserted only if the value is negative. CR and DB count toward the data-item size, occupy two character positions, and may only appear in the last character position of the edit PICTURE string.

  The Currency Symbol

  The currency symbol (usually $) must be one of the leading characters of the edit PICTURE string. It may be preceded by a plus or a minus sign.

  The default currency symbol is the dollar sign ($); but as shown in Listing 9-2, it may be changed to a different symbol by the CURRENCY SIGN IS clause, in the SPECIAL-NAMES paragraph, CONFIGURATION SECTION, ENVIRONMENT

  DIVISION.

  Listing 9-2. Using the CURRENCY SIGN Clause to Change the Currency Symbol

  IDENTIFICATION DIVISION.

  PROGRAM-ID. Listing9-2.

  AUTHOR. Michael Coughlan.

  ENVIRONMENT DIVISION.

  CONFIGURATION SECTION.

  SPECIAL-NAMES.

  188

  Chapter 9 ■ edited piCtures

  CURRENCY SIGN IS "£".

  DATA DIVISION.

  WORKING-STORAGE SECTION.

  01 Edit1 PIC £££,££9.99.

  PROCEDURE DIVISION.

  Begin.

  MOVE 12345.95 TO Edit1

  DISPLAY "Edit1 = " Edit1

  STOP RUN.

  In Listing 9-3, multiple currency sign declarations are used to create a currency converter program. Several CURRENCY SIGN declarations are made (note that while there are several clauses there is only one sentence and hence one period) and then each edited picture uses the appropriate currency symbol.

  Listing 9-3. Using Multiple CURRENCY SIZE clauses

  IDENTIFICATION DIVISION.

  PROGRAM-ID. Listing9-3.

  AUTHOR. Michael Coughlan.

  ENVIRONMENT DIVISION.

  CONFIGURATION SECTION.

  SPECIAL-NAMES.

  CURRENCY SIGN IS "£"

  CURRENCY SIGN IS "$"

  CURRENCY SIGN IS "¥".

  DATA DIVISION.

  WORKING-STORAGE SECTION.

  01 DollarValue PIC 9999V99.

  01 PrnDollarValue PIC $$$,$$9.99.

  01 PrnYenValue PIC ¥¥¥,¥¥9.99.

  01 PrnPoundValue PIC £££,££9.99.

  01 Dollar2PoundRate PIC 99V9(6) VALUE 0.640138.

  01 Dollar2YenRate PIC 99V9(6) VALUE 98.6600.

  PROCEDURE DIVISION.

  Begin.

  DISPLAY "Enter a dollar value to convert :- " WITH NO ADVANCING

  ACCEPT DollarValue

  MOVE DollarValue TO PrnDollarValue

  COMPUTE PrnYenValue ROUNDED = DollarValue * Dollar2YenRate

  COMPUTE PrnPoundValue ROUNDED = DollarValue * Dollar2PoundRate

  189

  Chapter 9 ■ edited piCtures

  DISPLAY "Dollar value = " PrnDollarValue

  DISPLAY "Yen value = " PrnYenValue

  DISPLAY "Pound value = " PrnPoundValu
e

  STOP RUN.

  Fixed-Insertion Examples

  Table 9-5 gives examples of fixed insertion using the plus and minus edit symbols. Table 9-6 does the same for the CR

  and DB edit symbols.

  Table 9-5. Fixed-Insertion Editing with the Plus and Minus Symbols

  Sending

  Receiving

  Comments

  Picture

  Data

  Picture

  Result

  PIC S9(4)

  –4174

  PIC –9999

  –4174

  A negative value moved into an item edited

  with a minus sign inserts the minus sign.

  Size = 5 characters.

  PIC S9(4)

  –4174

  PIC 9999–

  4174–

  When the minus sign is in the last character

  position, the minus is inserted into the last

  character position.

  Size = 5 characters.

  PIC S9(4)

  +4174

  PIC –9999

  4174

  A positive value moved into an item edited

  with a minus sign inserts a space character in

  the position of the minus symbol.

  Size = 5 characters.

  PIC S9(4)

  +4174

  PIC +9999

  +4174

  A positive value moved into an item edited

  with a plus sign inserts a plus sign.

  Size = 5 characters.

  PIC S9(4)

  –174

  PIC +9999

  –0174

  A negative value moved into an item edited

  with a plus sign inserts a minus sign.

  Size = 5 characters.

  PIC S9(4)

  –174

  PIC 9999+

  0174–

  A negative value moved into an item edited

  with a plus sign inserts a minus sign in the

  character position of the plus symbol.

  Size = 5 characters.

  190

  Chapter 9 ■ edited piCtures

  Table 9-6. Fixed-Insertion Editing with CR, DB, and the Currency Symbol

  Sending

  Receiving

  Comments

  Picture

  Data

  Picture

  Result

  PIC 999

  174

  PIC $9999

  $0174

  The $ sign is inserted.

  Size = 5 characters.

  PIC S999

  –174

  PIC –$9999

  –$0174

  The minus symbol is used before the $ and the

  value is negative, so both are inserted.

  Size = 6 characters.

  PIC S999

  –174

  PIC –$9999CR

  –$0174CR

  The minus symbol is used at the start of the edit

  string and CR at the end. The value is negative,

  so the minus and CR are both inserted.

  Size = 8 characters.

  PIC S999

  +174

  PIC +$9999CR

  +$0174

  A plus sign is used before the $ and the value

  is positive, so a plus sign is inserted. The

  CR symbol is used; but because the value is

  positive, spaces are inserted.

  Size = 8 characters.

  PIC S9(4)

  -4174

  PIC 9999CR

  4174CR

  The value is negative, so CR is inserted.

  Size = 6 characters.

  PIC S9(4)

  +4174

  PIC 9999CR

  4174

  The value is positive, so spaces are inserted.

  Size = 6 characters.

  PIC S9(4)

  –4174

  PIC 9999DB

  4174DB

  The value is negative, so DB is inserted.

  Size = 6 characters.

  PIC S9(4)

  +4174

  PIC 9999DB

  4174

  The value is positive, so spaces are inserted.

  Size = 6 characters.

  PIC S9(4)

  –174

  PIC 9999+

  0174–

  A negative value moved into an item edited

  with a plus sign inserts a minus sign in the

  character position of the plus symbol.

  Size = 5 characters.

  Floating Insertion

  The problem with fixed-insertion editing is that data formatted using it can be somewhat unsightly. Values like $00019,825.75 and -0000135 are more acceptably presented as $19,825.75 and -135. What makes these formats more presentable is that the leading zeros have been suppressed and the editing symbol has been “floated” up against the first non-zero digit. In COBOL, this effect can be achieved using floating insertion. Floating insertion can only be applied to numeric-edited data items.

  The floating-insertion symbols are the plus and minus signs and the currency symbol. Floating insertion

  suppresses leading zeros and floats the insertion symbol up against the first non-zero digit. Every floating symbol counts toward the size of the printed item. Each floating-insertion symbol—with the exception of the leftmost symbol, which is always inserted—is a placeholder that may be replaced by a space or a digit. This means at least one symbol is always inserted, even though this may be at the cost of truncating the number.

  191

  Chapter 9 ■ edited piCtures

  Floating-Insertion Examples

  The examples in Table 9-7 show how you can use floating-insertion editing. You should pay particular attention to how floating insertion deals with the comma when this symbol is combined with the floating-insertion symbols (see the second example).

  Table 9-7. Floating-Insertion Editing with Plus, Minus, and the Currency Symbol

  Sending

  Receiving

  Comments

  Picture

  Data

  Picture

  Result

  PIC 9(4)V99

  000000

  PIC $$,$$9.99

  ****$0.00

  The currency symbol floats against the

  digit in the leftmost numeric position. The

  zeros to the left of the currency symbol are

  replaced by spaces.

  Size = 9 characters.

  PIC 9(4)V99

  174.75

  PIC $$,$$9.99

  **$174.75

  The currency symbol replaces the comma

  because there are no digits to the left of the

  comma (rule prevents $,174.75).

  Size = 9 characters.

  PIC 9(4)V99

  4174.75

  PIC $$,$$9.99

  $4,174.75

  A comma is inserted.

  Size = 9 characters.

  PIC 9(4)V99

  4174.75

  PIC $$,$$9.00

  $4,174.00

  The zeros are inserted to the right of

  the decimal point. After decimal-point

  alignment, the least significant digits (75)

  of the value are lost.

  Size = 9 characters.

  PIC 9(5)V99

  34174.75

  PIC $$,$$9.99

  $4,174.75

  The leftmost currency symbol cannot be

  replaced by a digit, which means after

  alignment the most significant digit (3) is lost.

  Size = 9 characters.

  PIC S9(3)

  -26

  PIC +++9

  *-26

  The character positions to the left of the

  currency symbol are occupied by spaces.

  Size = 4 characters.

  PIC S9(3)

  +426

  PIC +++9

  +426

  Size = 4 characters.

  PIC S9(3)

/>   -426

  PIC ---9

  -426

  Size = 4 characters.

  PIC S9(3)

  +426

  PIC ---9

  426

  A positive value and the minus symbol

  mean the symbol is replaced by space.

  Size = 4 characters.

  PIC S9(4)

  +6426

  PIC +++9

  +426

  The leftmost plus symbol cannot be replaced

  by a digit, which means after alignment the

  most significant digit (6) is lost.

  Size = 4 characters.

  192

  Chapter 9 ■ edited piCtures

  Suppression-and-Replacement Editing

  Suppression-and-replacement editing is used to replace leading zeroes from the value to be edited with the replacement symbol. Like floating insertion, suppression and replacement can only be applied to numeric-edited data items. There are two varieties of suppression-and-replacement editing:

  • Suppression of leading zeros and replacement with spaces

  • Suppression of leading zeros and replacement with asterisks

  The suppression and replacement symbols are the letter Z and the asterisk (*). Using Z in an edited picture instructs the computer to suppress a leading zero in that character position and replace it with a space. Using * in an edited picture instructs the computer to replace a leading zero with an asterisk. The picture clause symbol 9 cannot appear to the left of the replacement symbols (Z or *). If all the character positions in a data item are Z editing symbols and the sending item is 0, then only spaces will be printed. Replacement with spaces is done for aesthetic reasons, but replacement with asterisks is often done as a security measure on checks.

  Suppression-and-Replacement Examples

  Table 9-8 shows how you can use suppression and replacement. As with the examples in Table 9-7, you should pay particular attention to how suppression-and-replacement editing deals with the comma when this symbol is combined with the replacement symbols.

  Table 9-8. Suppression-and-Replacement Editing Examples

  Sending

  Receiving

  Comments

  Picture

  Data

  Picture

  Result

  PIC 9(4)

  0000

  PIC Z,ZZZ

  

  The value is zero, and the edit string instructs the

  computer to replace the leading zeros with spaces.

  Size= 5 characters.

  PIC 9(4)

  8317

  PIC Z,Z99

  8,317

  If there are no leading zeros, there is no replacement.

  Size= 5 characters.

 

‹ Prev