MainframeMaster

COBOL Tutorial

COBOL RIGHT Clause - Quick Reference

Progress0 of 0 lessons

Overview

The RIGHT clause is used to right-align data within a field during MOVE and STRING operations. It positions data at the right end of the target field, with spaces (or padding characters) filling the left side.

Purpose and Usage

  • Data alignment - Right-align data within fields
  • Formatting - Create professional-looking output
  • Report generation - Align columns in reports
  • Currency formatting - Format monetary amounts
  • Numeric display - Align numeric data properly

Alignment Concept

Field: [1234567890] (10 characters)
Data: "ABC"
LEFT: [ABC ]
RIGHT: [ ABC]
RIGHT positions data at the end

RIGHT aligns data to the right side with left padding.

Syntax

The RIGHT clause follows specific syntax patterns in MOVE and STRING operations.

Basic Syntax

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
* Basic RIGHT clause syntax MOVE source-data TO target-field RIGHT * With STRING operation STRING source1 DELIMITED BY SIZE source2 DELIMITED BY SIZE INTO target-field RIGHT * Complete example IDENTIFICATION DIVISION. PROGRAM-ID. RIGHT-ALIGNMENT-EXAMPLE. DATA DIVISION. WORKING-STORAGE SECTION. 01 SOURCE-DATA PIC X(5) VALUE "ABC". 01 TARGET-FIELD PIC X(10). 01 CURRENCY-AMOUNT PIC 9(6)V99 VALUE 123456. 01 FORMATTED-AMOUNT PIC X(12). PROCEDURE DIVISION. MAIN-LOGIC. * Right-align text data MOVE SOURCE-DATA TO TARGET-FIELD RIGHT DISPLAY "Right-aligned: [" TARGET-FIELD "]" * Right-align currency amount MOVE CURRENCY-AMOUNT TO FORMATTED-AMOUNT RIGHT DISPLAY "Currency: [" FORMATTED-AMOUNT "]" STOP RUN.

RIGHT can be used with MOVE and STRING operations for alignment.

RIGHT vs LEFT Comparison

AlignmentPositionPadding
RIGHTRight endLeft side
LEFTLeft endRight side
DefaultLeft endRight side

Practical Examples

These examples demonstrate how to use the RIGHT clause effectively in different formatting scenarios.

Currency Formatting

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
IDENTIFICATION DIVISION. PROGRAM-ID. CURRENCY-FORMATTING. DATA DIVISION. WORKING-STORAGE SECTION. 01 AMOUNT PIC 9(6)V99 VALUE 123456. 01 FORMATTED-AMOUNT PIC X(12). 01 DISPLAY-AMOUNT PIC X(15). PROCEDURE DIVISION. MAIN-LOGIC. * Right-align the amount MOVE AMOUNT TO FORMATTED-AMOUNT RIGHT * Create display with currency symbol STRING "$" DELIMITED BY SIZE FORMATTED-AMOUNT DELIMITED BY SIZE INTO DISPLAY-AMOUNT RIGHT DISPLAY "Amount: [" DISPLAY-AMOUNT "]" * Format multiple amounts PERFORM FORMAT-AMOUNTS STOP RUN. FORMAT-AMOUNTS. MOVE 100 TO AMOUNT MOVE AMOUNT TO FORMATTED-AMOUNT RIGHT DISPLAY "Small amount: [" FORMATTED-AMOUNT "]" MOVE 999999 TO AMOUNT MOVE AMOUNT TO FORMATTED-AMOUNT RIGHT DISPLAY "Large amount: [" FORMATTED-AMOUNT "]" MOVE 0 TO AMOUNT MOVE AMOUNT TO FORMATTED-AMOUNT RIGHT DISPLAY "Zero amount: [" FORMATTED-AMOUNT "]".

RIGHT alignment creates professional currency formatting.

Report Column Alignment

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
* Report formatting with RIGHT alignment DATA DIVISION. WORKING-STORAGE SECTION. 01 REPORT-LINE PIC X(80). 01 ITEM-NAME PIC X(20) VALUE "Product A". 01 QUANTITY PIC 9(4) VALUE 123. 01 PRICE PIC 9(6)V99 VALUE 456789. 01 TOTAL PIC 9(8)V99. PROCEDURE DIVISION. GENERATE-REPORT. * Calculate total MULTIPLY QUANTITY BY PRICE GIVING TOTAL * Create formatted report line STRING ITEM-NAME DELIMITED BY SIZE SPACES DELIMITED BY SIZE QUANTITY DELIMITED BY SIZE SPACES DELIMITED BY SIZE PRICE DELIMITED BY SIZE SPACES DELIMITED BY SIZE TOTAL DELIMITED BY SIZE INTO REPORT-LINE RIGHT DISPLAY REPORT-LINE * Alternative: Format each field separately PERFORM FORMAT-REPORT-LINE. FORMAT-REPORT-LINE. * Right-align each column MOVE ITEM-NAME TO REPORT-LINE(1:20) RIGHT MOVE QUANTITY TO REPORT-LINE(25:4) RIGHT MOVE PRICE TO REPORT-LINE(35:8) RIGHT MOVE TOTAL TO REPORT-LINE(50:10) RIGHT DISPLAY "Formatted: [" REPORT-LINE "]".

RIGHT alignment creates properly aligned report columns.

Percentage Formatting

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
* Percentage formatting with RIGHT DATA DIVISION. WORKING-STORAGE SECTION. 01 PERCENTAGE PIC 9(3)V99 VALUE 12345. 01 FORMATTED-PCT PIC X(8). 01 DISPLAY-PCT PIC X(10). PROCEDURE DIVISION. FORMAT-PERCENTAGES. * Right-align percentage MOVE PERCENTAGE TO FORMATTED-PCT RIGHT * Add percent symbol STRING FORMATTED-PCT DELIMITED BY SIZE "%" DELIMITED BY SIZE INTO DISPLAY-PCT RIGHT DISPLAY "Percentage: [" DISPLAY-PCT "]" * Format different percentages MOVE 50 TO PERCENTAGE MOVE PERCENTAGE TO FORMATTED-PCT RIGHT STRING FORMATTED-PCT DELIMITED BY SIZE "%" DELIMITED BY SIZE INTO DISPLAY-PCT RIGHT DISPLAY "50%: [" DISPLAY-PCT "]" MOVE 999 TO PERCENTAGE MOVE PERCENTAGE TO FORMATTED-PCT RIGHT STRING FORMATTED-PCT DELIMITED BY SIZE "%" DELIMITED BY SIZE INTO DISPLAY-PCT RIGHT DISPLAY "999%: [" DISPLAY-PCT "]".

RIGHT alignment formats percentages consistently.

Date and Time Formatting

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
* Date and time formatting with RIGHT DATA DIVISION. WORKING-STORAGE SECTION. 01 CURRENT-DATE PIC X(8). 01 CURRENT-TIME PIC X(6). 01 FORMATTED-DATE PIC X(12). 01 FORMATTED-TIME PIC X(10). PROCEDURE DIVISION. FORMAT-DATE-TIME. * Get current date and time MOVE FUNCTION CURRENT-DATE TO CURRENT-DATE MOVE FUNCTION CURRENT-TIME TO CURRENT-TIME * Right-align date components STRING CURRENT-DATE(1:4) DELIMITED BY SIZE "-" DELIMITED BY SIZE CURRENT-DATE(5:2) DELIMITED BY SIZE "-" DELIMITED BY SIZE CURRENT-DATE(7:2) DELIMITED BY SIZE INTO FORMATTED-DATE RIGHT * Right-align time components STRING CURRENT-TIME(1:2) DELIMITED BY SIZE ":" DELIMITED BY SIZE CURRENT-TIME(3:2) DELIMITED BY SIZE ":" DELIMITED BY SIZE CURRENT-TIME(5:2) DELIMITED BY SIZE INTO FORMATTED-TIME RIGHT DISPLAY "Date: [" FORMATTED-DATE "]" DISPLAY "Time: [" FORMATTED-TIME "]" * Create timestamp STRING FORMATTED-DATE DELIMITED BY SIZE " " DELIMITED BY SIZE FORMATTED-TIME DELIMITED BY SIZE INTO FORMATTED-DATE RIGHT DISPLAY "Timestamp: [" FORMATTED-DATE "]".

RIGHT alignment creates consistent date/time formatting.

Best Practices and Considerations

Understanding best practices ensures effective use of the RIGHT clause.

Best Practices

  • Use for numeric data - RIGHT alignment is standard for numbers
  • Consider field size - Ensure target field is large enough
  • Be consistent - Use RIGHT alignment consistently throughout
  • Test formatting - Verify alignment with various data lengths
  • Document usage - Clearly specify when RIGHT is used
  • Consider readability - Ensure aligned data is easy to read

Common Use Cases

Use CaseDescriptionExample
Currency AmountsRight-align monetary values$1,234.56
PercentagesRight-align percentage values25.5%
Report ColumnsAlign numeric columns in reportsQuantity: 123
Dates and TimesFormat date/time displays2023-12-25
Account NumbersRight-align account identifiersAccount: 123456

Performance Considerations

  • Minimal impact - RIGHT alignment has negligible performance effect
  • Formatting operation - Only affects data movement time
  • Field size dependent - Performance varies with field size
  • Output formatting - Most commonly used in display operations
  • Memory usage - No additional memory requirements

RIGHT Clause Quick Reference

UsageSyntaxExample
Basic RIGHTMOVE source TO target RIGHTMOVE AMOUNT TO FIELD RIGHT
With STRINGSTRING data INTO target RIGHTSTRING A B INTO C RIGHT
Currency formattingMOVE amount TO field RIGHTMOVE 123456 TO CURRENCY RIGHT
Report alignmentMOVE data TO column RIGHTMOVE QUANTITY TO COLUMN RIGHT
PercentageMOVE pct TO field RIGHTMOVE 25.5 TO PERCENT RIGHT

Test Your Knowledge

1. What is the primary purpose of the RIGHT clause in COBOL?

  • To align text to the right side of a field
  • To extract the rightmost characters from a string
  • To perform right arithmetic operations
  • To move data to the right

2. In which COBOL statements is the RIGHT clause commonly used?

  • MOVE and STRING statements
  • READ and WRITE statements
  • ADD and SUBTRACT statements
  • IF and EVALUATE statements

3. What happens when you use RIGHT with a MOVE operation?

  • Data is moved to the rightmost position
  • Data is right-aligned with left padding
  • Data is reversed
  • Data is truncated

4. How does RIGHT differ from LEFT alignment?

  • RIGHT is faster than LEFT
  • RIGHT aligns to the right, LEFT aligns to the left
  • RIGHT can only be used with numbers
  • There is no difference

5. What is the default alignment when RIGHT is not specified?

  • RIGHT alignment
  • LEFT alignment
  • CENTER alignment
  • No alignment

Frequently Asked Questions