Decimal Point Data Type
- The decimal values (the numeric values with a fractional part) are also part of numeric calculations.
- When an input 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 part of the decimal variable declaration and is used to display the decimal point while displaying the decimal value.
- These variables won't be used for arithmetic calculations; the dot is counted as part of the variable length.
- The presence of the "." in the PIC causes a real decimal to be stored 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.
- A "V" in the PICTURE clause indicates the assumed or implied decimal point.
Notes -
- Basically, the "V" is not part of the data and does not count in the variable length. COBOL assumes that the dot is present in the value where the V is coded in the declaration and uses 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 numeric variables, including a packed or comp-3.
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 -
The assumed decimal scaling position is used to code the location of an assumed decimal point when the decimal point is not within the number. It is represented by the 'P'.
Notes -
- It is not counted as part of the variable size.
- 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 a leftmost character in the PIC clause, the fraction after the decimal point is moved to the variable.
- If P is coded as the rightmost character in the PIC clause, the whole number before the decimal point is moved to the variable.
Practical Example -
Scenario - Below example describes about the decimal point data type declaration and usage in COBOL programming.
Code -
----+----1----+----2----+----3----+----4----+----5----+
...
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.
...
Output -
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