Decimal Point Data Type
The decimal values (the numeric values with fractions) are also part of numeric calculations. Like all programming languages, COBOL has its own way of declarations to handle the decimal values in programming.
When an input to the program is decimal, a variable should declare with decimal point to handle the decimal value. Decimal point declares only with the combination of numeric data type.
The Decimal point declarations are two types -
- Real Decimal Point | Dot
- Implied Decimal Point | Assumed Decimal Point
Real Decimal Point | Dot -
Dot | period (.) is a part of decimal variable declaration and used to display the decimal point while displaying the decimal variable. The only purpose of this declaration is to display the decimal value with a Dot and these variables wont use for arithmetic calculations. The Dot is counted as part of the variable length.
The presence of the "." in the PIC causes storing a real decimal in the memory. For example - the real decimal variable declaration to store the decimal value 1234.55 is as follows -
01 WS-DECIMAL-VAL PIC 9(4).9(2) VALUE 1234.55.
WS-DECIMAL-VALUE contains 1234.55. When we display WS-DECIMAL-VAL, 1234.55 gets displayed and there is no change in the display.
Assumed Decimal Point | Implied Decimal Point -
Assumed Decimal Point is used with a decimal variable declaration to use the decimal value in arithmetic calculations. The assumed or implied decimal point is indicated by a "V" in the PICTURE clause.
- Basically, the "V" is not part of the data and do not count in the variable length. COBOL assumes that the dot is the present in the value where the V is coded in the declaration and use the value accordingly in calculations.
- "V" is coded at a specified location in a field, but not actually stored.
- Assumed decimal can apply to any kind of numeric field including a packed or comp-3 field.
For example - the assumed decimal declaration to store the decimal value 1234.55.
01 WS-DECIMAL-VAL PIC 9(4)V9(2) VALUE 1234.55.
WS-DECIMAL-VALUE contains 1234.55. When we display WS-DECIMAL-VAL, it displays as 123455 and the decimal point ignores in display. However, the value in the WS-DECIMAL-VAL is 1234.55.
Assumed decimal scaling position -
Assumed decimal scaling position is used to code the location of an assumed decimal point when the decimal point is not within the number. Assumed decimal scaling position is represented by the 'P'.
- It is not counted in the size of the variable.
- Scaling position characters are counted to determine the maximum number of digit positions in numeric-edited items or in arithmetic operands.
- If P is coded as left-most character in PIC clause, the fraction after the decimal point moved to the variable.
- If P is coded as right-most character in PIC clause, the number before the decimal point moved to the variable.
Practical Example -
Scenario - Below example describes about the decimal point data type declaration and usage in COBOL programming.
----+----1----+----2----+----3----+----4----+----5----+ IDENTIFICATION DIVISION. PROGRAM-ID. ADECPDT. AUTHOR. MTH. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-VARS. 05 WS-REAL-DP PIC 9(03).9(2). 05 WS-ASSUM-DP PIC 9(03)V9(2). 05 WS-ASSUM-DSPL PIC 9(03)P. 05 WS-ASSUM-DSPR PIC P9(02). PROCEDURE DIVISION. MOVE 123.45 TO WS-REAL-DP WS-ASSUM-DP WS-ASSUM-DSPL WS-ASSUM-DSPR. DISPLAY "DISLAY FOR 9(03).9(2): " WS-REAL-DP. DISPLAY "DISLAY FOR 9(03)V9(2): " WS-ASSUM-DP. DISPLAY "DISLAY FOR 9(03)P : " WS-ASSUM-DSPL. DISPLAY "DISLAY FOR P9(02) : " WS-ASSUM-DSPR. STOP RUN.
DISLAY FOR 9(03).9(2): 123.45 DISLAY FOR 9(03)V9(2): 12345 DISLAY FOR 9(03)P : 012 DISLAY FOR P9(02) : 50
Explaining example -
In the above example:
- The real decimal variable WS-REAL-DP displays the value with a decimal point.
- The assumed decimal variable WS-ASSUM-DP displays value without decimal; however, it uses the decimal value in all calculations.
- Assumed decimal scaling variable WS-ASSUM-DSPL displays value 12 and ignores the rightmost digit before the decimal because of the right justification.
- Assumed decimal scaling variable WS-ASSUM-DSPR displays value 5 and ignores the leftmost digit before the decimal because of the left justification after the decimal.