Sunday, 26 March 2023

What is Packed-Decimal in COBOL: A Comprehensive Guide. | USAGE Is PACKED-DECIMAL (COMP-3).

COBOL COMP-3, COMPUTATIONAL-3

What is Packed-Decimal in COBOL?

Welcome back to another exciting blog post on Packed-Decimal in COBOL, Computational-3, or Comp-3. If you're working as a COBOL programmer or aspiring to be one, you've probably come across the term Packed-Decimal (COMP-3) or USAGE Clause in COBOL

In this article, we'll explain what is PACKED-DECIMAL in COBOL, how it works, and how you can define COBOL data items with USAGE IS PACKED-DECIMAL or COMP-3. Let's deep dive into the COBOL packed-decimal (COMP-3) Clause.  

Introduction to COBOL COMP-3.


The term COBOL stands for Common Business-Oriented Language. It's one of the oldest and most robust programming languages. COBOL is still used heavily for enterprise computing since its introduction in the late 1950s. One of the unique features of COBOL is its support for various data types, including Packed-Decimal. 

In fact, it provides more control to the programmer on how to store and process data in the COBOL programs. They can use usage is packed-decimal to store data in compact form to save disk space.  

What is Packed-Decimal in COBOL or COMP-3?


Packed decimal format, also known as COMP-3, is a binary-coded decimal data (BCD) type in COBOL. It is used to represent decimal numbers in a compact format and is primarily used for arithmetic calculations.  

Packed-Decimal in COBOL or Computation-3 enables the computers to store two digits in each storage position. The rightmost position is used to store the sign (+/-). The following figure showcase how data is stored in the computer when the variable is defined in COMP-3 format (PACKED-DECIMAL in COBOL). 

COBOL Packed-Decimal.
COBOL Packed-Decimal Representation.


All other bytes hold two digits each. As a result, each packed-decimal number has an odd number of digits and a sign.

Packed-decimal format (Packed-Decimal or Comp-3 usage)
  • 00 12 3F  Unsigned number with a value of    123
  • 00 12 3C  Positive   number with a value of  +123
  • 00 12 3D  Negative number with a value of   -123
Note: This format is most efficient when you code an odd number of digits in the PICTURE description so that the leftmost byte is fully used. Packed-decimal items are handled as fixed-point numbers for arithmetic purposes.

How is Packed-Decimal used in COBOL?


Cobol programs commonly use packed decimals (COMP-3) for financial calculations and data processing. It is particularly useful when dealing with large quantities of numerical data, such as bank account balances or sales figures. Furthermore, packed-decimal fields can store dates and times in a compact form.

Declaring Packed-Decimal Variables in COBOL


To declare a Packed-Decimal variable in COBOL, you need to use the PIC clause. The PIC clause specifies the data type and size of the variable. Here is an example of a Packed-Decimal variable declaration:

01 ACCOUNT-BALANCE     PIC    S9(05) COMP-3.
01 TOTAL-TAX           PIC    S9(05). 

In this example, ACCOUNT-BALANCE is the variable name, S9(5) specifies that the variable can hold a maximum of five digits, and COMP-3 indicates that the data type is Packed-Decimal.

The field ACCOUNT-BALANCE will take only 3 storage positions (i.e., 3 bytes)  because data is stored in the compact form. However, the field TOTAL-TAX would require 5 bytes because it's defined without any usage clause, so the default (Usage is Display) will be applied. 

COMP-3 Size Calculation - Formula. 


The following formula is used to calculate the storage required by the variable which is defined as a packed decimal in COBOL. 

Formula: N/2 +1 
  • N - Represent the number of digits that a variable can store.
  • Discard, the remainder or the decimal portion from the result before adding 1 to the result. 
Lastly, you should never use packed decimal format or COBOL COMP-3 for printing data. if you try to print the COMP-3 variable in the report of spool then the output would be in non-readable format because data is stored in a compact format.  

In order to print any COMP-3 variable, you must move the field to the corresponding variable which is defined as Usage is Display or report variables.  

Benefits of Packed-Decimal in COBOL


There are several advantages of using Packed-Decimal in COBOL:
  • Space Efficiency: Packed-Decimal fields require less storage space than other numeric data types, making them ideal for handling large amounts of numerical data.
  • Fast Calculations: Packed-Decimal fields can be processed quickly and efficiently since they use a binary format that is easy for the computer to manipulate.
  • Precision: Packed-Decimal fields offer high precision since they can represent decimal numbers without rounding errors.
  • Compatibility: Packed-Decimal is a standard data type in COBOL, which means that it can be easily exchanged between different systems and platforms.

FAQs: COBOL COMP-3.

Q. How is Packed-Decimal different from other data types in COBOL?

A. Packed-Decimal is a binary-coded decimal data type that uses a single byte for each decimal digit, making it more space-efficient than other numeric data types.

Q. What is the maximum number of digits that can be stored in a Packed-Decimal field?

A. The maximum number of digits that can be stored in a Packed-Decimal field depends on the number of bytes allocated to it. In COBOL, a Packed-Decimal field is defined using the PIC clause with a COMP-3 format. A single byte in a Packed-Decimal field can store two decimal digits and can store up to 18 digits.

Summary. 

Created with Artisteer
In conclusion, packed decimal in COBOL is a widely used data format for storing and processing numeric data. It offers a compact way of representing large numbers and makes arithmetic computations more efficient. In fact, being a COBOL programmer, you must have a good understanding of how to define and manipulate packed-decimal data in your COBOL program especially when you are dealing with financial and other numeric data-intensive applications. By utilizing the correct data formats and optimizations, you can improve the efficiency of the COBOL programs. 

Subscribe to Topictrick & Don't forget to press THE BELL ICON to never miss any updates. Also, Please visit mention the link below to stay connected with Topictrick and the Mainframe forum on - 

► Youtube
► Facebook 
► Reddit

Thank you for your support. 

Mainframe Forum™

No comments:

Post a Comment

New In-feed ads