Summary -

In this topic, we described about the Numeric Data Type with detailed example.

Numeric data type allows to declare the data items to store the numeric decimal values. Numeric values are the combination of 0 to 9 numbers. Numeric data type uses character ‘9’ in PICTURE clause of data description to represent the decimal digits.

Each digit in the number should have counted and specified with data type definition using PICTURE clause. For example - Declaring a data item to store 3-digit value should have the declaration as 999 or 9(3).

The numeric data type maximum length is 18 bytes or can code up to 18 digits in the PICTURE clause (9(18)) using the default compiler option ARITH(COMPAT). If using ARITH(EXTEND), up to 31 digits can code in the PICTURE clause.

Numeric data type can code with a combination of other data types P(actual decimal point), S(sign) and V(virtual decimal point).

All USAGE clauses are applicable for Numeric data type. The variable length is calculated based on the USAGE clause used during the declaration.

If no USAGE clause specified during the declaration, DISPLAY computation applied to the declaration. i.e., 1 digit = 1 byte.

Alignment / Justification -

Numeric data type is right justified by default and the data in all numeric data items or variables are right justified automatically.

If the sending data is larger than the receiving data item, the leftmost digits are truncated.

If the sending data is smaller than the receiving data item, the unused character positions at the left are filled with zeroes.


Practical Example -

Scenario - Below example describes about the numeric data type declaration, justification and truncation in COBOL programming.

Code -

Numeric Data Type program Code
=COLS> ----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
****** ***************************** Top of Data ******************************
000001        IDENTIFICATION DIVISION.                                         
000002        PROGRAM-ID. NUMERICD.                                            
000003        AUTHOR. MTH.                                                     
000004                                                                         
000005        DATA DIVISION.                                                   
000006        WORKING-STORAGE SECTION.                                         
000007                                                                         
000008        01 WS-NUM-SVAR   PIC 9(05).                                      
000009        01 WS-NUM-LVAR   PIC 9(10).                                      
000010                                                                         
000011        PROCEDURE DIVISION.                                              
000012                                                                         
000013            MOVE 1234567        TO  WS-NUM-SVAR                          
000014                                    WS-NUM-LVAR.                         
000015                                                                         
000016            DISPLAY "WS-NUM-SVAR:  " WS-NUM-SVAR.                        
000017            DISPLAY "WS-NUM-LVAR:  " WS-NUM-LVAR.                        
000018                                                                         
000019            STOP RUN.                                                    
****** **************************** Bottom of Data ****************************

Output -

Numeric Data Type program Output