Master Mainframe Technologies - COBOL, JCL, DB2, VSAM, CICS & More
ABEND Codes SQLCODEs File Status Interview Prep Contact
💻 COBOL

COBOL Sign Handling

Beginner 🕑 8 min read 👁 0 views

COBOL Sign Handling

Understand how COBOL stores and processes numeric signs.

SIGN Clause

```cobol 01 WS-AMOUNT PIC S9(5)V99 SIGN IS LEADING SEPARATE. 01 WS-VALUE PIC S9(5)V99 SIGN IS TRAILING. ```

Sign Storage Options

  • TRAILING (default): Sign in last byte
  • LEADING: Sign in first byte
  • SEPARATE: Sign in own byte (+/-)

Zoned Decimal Signs

Without SEPARATE, sign is combined with digit:

  • Positive: {ABCDEFGHI (0-9)
  • Negative: }JKLMNOPQR (0-9)

SIGN Display

```cobol 01 WS-DISPLAY PIC -Z,ZZZ,ZZ9.99. Leading minus if negative 01 WS-DISPLAY PIC +Z,ZZZ,ZZ9.99. Sign always shows 01 WS-DISPLAY PIC Z,ZZZ,ZZ9.99CR. CR if negative ```

NUMVAL with Signs

```cobol COMPUTE WS-NUM = FUNCTION NUMVAL('-123.45'). ```

Best Practices

  • Use SIGN LEADING SEPARATE for external files
  • Match sign conventions with trading partners
  • Consider USAGE PACKED-DECIMAL for efficiency

Code Example

       IDENTIFICATION DIVISION.
       PROGRAM-ID. SIGN-DEMO.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-NUM1 PIC S9(5)V99 VALUE -1234.56.
       01 WS-NUM2 PIC S9(5)V99 SIGN LEADING VALUE -1234.56.
       01 WS-NUM3 PIC S9(5)V99 SIGN LEADING SEPARATE
                  VALUE -1234.56.
       01 WS-DISP1 PIC -(5)9.99.
       01 WS-DISP2 PIC +(5)9.99.
       01 WS-DISP3 PIC Z(5)9.99-.
       PROCEDURE DIVISION.
           MOVE WS-NUM1 TO WS-DISP1
           MOVE WS-NUM1 TO WS-DISP2
           MOVE WS-NUM1 TO WS-DISP3
           DISPLAY 'Trailing sign (internal): ' WS-NUM1
           DISPLAY 'Leading sign: ' WS-NUM2
           DISPLAY 'Separate sign: ' WS-NUM3
           DISPLAY 'Leading - : ' WS-DISP1
           DISPLAY 'Leading +/-: ' WS-DISP2
           DISPLAY 'Trailing - : ' WS-DISP3
           STOP RUN.