Numeric Data Type


  • The numeric data type is used to declare variables to store and process numbers.
  • It allows numbers that are a combination of 0 to 9 numbers.
  • It uses the number '9' as a PICTURE symbol to declare variables.
  • Each digit in the number has to be counted to calculate the length, which should be coded after the PICTURE symbol 9. For Example - Declaring a variable to store a 3-digit value should have the declaration as 999 or 9(3).
  • A numeric variable can store maximum of 18 digits.

Syntax -

----+----1----+----2----+----3----+----4----+----5----+
       01 ws-variable      PIC 9(var-length) 
	                      [VALUE number].
Note! All statements coded in [ ] are optional.

Example -

01 WS-VAR      PIC 9(03) VALUE 123.
  • ws-variable - specifies the variable name. From the above example, it is WS-VAR.
  • var-length - specifies the number of digits in the value. The maximum length is 18 digits. From the above example, it is 03.
  • input-string - specifies the string assigned to the variable during the declaration. From the above example, it is 123.

Rules -

  • The maximum length of the numeric data type is 18 digits in the PICTURE clause (9(18)) using the default compiler option ARITH(COMPAT). If using ARITH(EXTEND), up to 31 digits can be coded in the PICTURE clause.
  • It can code with other data types, such as P(actual decimal point), S(sign), and V(virtual decimal point).
  • All USAGE clauses are applicable for numeric data types. The variable length is calculated based on the USAGE clause used during the declaration.
  • If no USAGE clause is coded, DISPLAY computation is applied to the declaration. i.e., i.e., 1 character = 1 byte.

Alignment | Justification -


  • The numeric data type is right justified by default, and the data in all numeric variables are right justified automatically.
  • The leftmost digits are truncated if the input is larger than the receiving variable size. For example – the variable declared with 9(4) and the input data is 12345, the variable contains only 2345.
  • If the input is smaller than the receiving variable size, unused character positions at the left are filled with ZEROs.

Practical Example -


Scenario - Example describes the numeric variable declaration, justification, and truncation.

Code -

----+----1----+----2----+----3----+----4----+----5----+
       ...
       WORKING-STORAGE SECTION.
       01 WS-VAR.
	  * Variable with shorter length than the passing input data
	      05 WS-NUM-SVAR   PIC 9(03).
	  * Variable with larger length than the passing input data
          05 WS-NUM-LVAR   PIC 9(09). 
       ...
       PROCEDURE DIVISION.
           MOVE 123456   TO  WS-NUM-SVAR 
                             WS-NUM-LVAR.

           DISPLAY "WS-NUM-SVAR:  -" WS-NUM-SVAR "-". 
           DISPLAY "WS-NUM-LVAR:  -" WS-NUM-LVAR "-". 
		   ...

Output -

WS-NUM-SVAR:  -456-       
WS-NUM-LVAR:  -000123456- 
Note! In the above example, '-' is used to show the boundaries of the data.