Processing Data L 4
Objectives
After completing this lesson, you will be able to:
Perform arithmetic calculations.
Apply string processing.
Arithmetic Calculations
Arithmetic expressions are ABAP expressions with a combination of values, operators, and functions that the runtime system processes to calculate a result. For arithmetic expressions the result type depends on the type of the operands used as input to the expression.
You can use an arithmetic expression in any reading operand position, for example, the right-hand side of a value assignment.
The following video illustrates the basics of arithmetic expressions.
Try It Out: Arithmetic Calculations
Like in the first exercise of this course, create a new global class that implements interface IF_OO_ADT_CLASSRUN.
Copy the following code snippet to the implementation part of method if_oo_adt_classrun~main( ):
Code Snippet
Copy code
Switch to dark mode
* Declarations
**********************************************************************
" comment/uncomment these line for different result types
TYPES t_result TYPE p LENGTH 8 DECIMALS 2.
* TYPES t_result TYPE p LENGTH 8 DECIMALS 0.
* TYPES t_result TYPE i.
DATA result TYPE t_result.
* Calculations
**********************************************************************
" comment/uncomment these lines for different calculations
result = 2 + 3.
* result = 2 - 3.
result = 2 3.
* result = 2 / 3.
*
* result = sqrt( 2 ).
* result = ipow( base = 2 exp = 3 ).
*
result = ( 8 7 - 6 ) / ( 5 + 4 ).
result = 8 7 - 6 / 5 + 4.
* Output
**********************************************************************
out->write( result ).
Press CTRL + F3 to activate the class and F9 to execute it as a console app.
Play around with the source code to get familiar with the concepts.
Uncomment different calculations and type definitions.
Implement your own calculations.
String Processing
String templates are ABAP expressions of result type string. You can use string templates in any reading operand position, for example, the right-hand side of a value assignment.
A string template begins and ends with a pipe-symbol ( | ). The simplest possible string template contains nothing but literal text. In this form a string template is not really different from a string literal.
What distinguishes a string template from a string literal is the ability to embed expressions. An embedded expression is an ABAP expression surrounded by a pair of curly brackets ( { and } ). At runtime, ABAP evaluates the embedded expression and translates the result into a string. In the final result, this string replaces the embedded expression (together with the surrounding curly brackets).
Note
ABAP syntax requires at least one blank after the opening bracket and at least one blank before the closing bracket.
Of course one string template can contain more than one embedded expression.
Inside the curly brackets you can place any kind of ABAP expression: arithmetic expressions, like in the example above, but single variables or even literals can serve as embedded expressions.
One important use case for string templates is the controlled formatting of data for output.
In the first example, variable the_date is of type d and contains a date in the internal (raw) format YYYYMMDD (where YYYY stands for the year, MM for the two-digit month and DD for the two-digit date). When you use variable the_date as embedded expression in a string template the result will be the same as the internal format. But when you add format option DATE = <date_format> within the curly brackets, the system will format the value as a date. If you add DATE = ISO, the output will be in ISO format. With DATE = USER the output format depends on the user settings of the current user.
The second example illustrates some of the options you can use for formatting numbers. Using NUMBER you control the general formatting of numbers, for example, whether a decimal point is used or a decimal comma. Using SIGN you control the position of the sign and whether a plus sign (+) is displayed or not. Using STYLE you can choose from several pre-defined styles, like a scientific style or an engineering style.
You can join fields together using the concatenation operator &&. You can join any combination of data objects and string expressions.
The constituent parts of the expression are joined with no space or other separator between them. If you need spaces or another separator character, you must remember to insert it yourself as part of the expression as shown in the second example.
Try It Out: String Processing
Like in the first exercise of this course, create a new global class that implements interface IF_OO_ADT_CLASSRUN.
Copy the following code snippet to the implementation part of method if_oo_adt_classrun~main( ):
Code Snippet
Copy code
Switch to dark mode
METHOD if_oo_adt_classrun~main.
* Declarations
**********************************************************************
TYPES t_amount TYPE p LENGTH 8 DECIMALS 2.
DATA amount TYPE t_amount VALUE '3.30'.
DATA amount1 TYPE t_amount VALUE '1.20'.
DATA amount2 TYPE t_amount VALUE '2.10'.
DATA the_date TYPE d VALUE '19891109'.
DATA my_number TYPE p LENGTH 3 DECIMALS 2 VALUE '-273.15'.
DATA part1 TYPE string VALUE Hello.
DATA part2 TYPE string VALUE World.
* String Templates
**********************************************************************
" comment/uncomment the following lines for different examples
DATA(text) = |Hello World|.
* DATA(text) = |Total: { amount } EUR|.
* DATA(text) = |Total: { amount1 + amount2 } EUR|.
* Format Options
**********************************************************************
"Date
* DATA(text) = |Raw Date: { the_date }|.
* DATA(text) = |ISO Date: { the_date Date = ISO }|.
* DATA(text) = |USER Date:{ the_date Date = USER }|.
"Number
* DATA(text) = |Raw Number { my_number }|.
* DATA(text) = |User Format{ my_number NUMBER = USER }|.
* DATA(text) = |Sign Right { my_number SIGN = RIGHT }|.
* DATA(text) = |Scientific { my_number STYLE = SCIENTIFIC }|.
* String expression (concatenation Operator)
**********************************************************************
* DATA(text) = part1 && part2.
* DATA(text) = part1 && | | && part2.
* DATA(text) = |{ amount1 } + { amount2 }| &&
* | = | &&
* |{ amount1 + amount2 }|.
* Output
**********************************************************************
out->write( text ).
Press CTRL + F3 to activate the class and F9 to execute it as a console app.
Play around with the source code to get familiar with the concepts.
Uncomment different value assignments for inline-declared variable text to see different result.
Try your own assignements.
Declare Variables and Process Data
In this exercise, you create a program that does a calculation based on two numbers, then formats and outputs the result.
Template:
none
Solution:
/LRN/CL_S4D400_BTS_COMPUTE (global Class)
Task 1: Variable Declaration
In your own package, create a new global class and define numeric variables for the input.
Steps
Create a new global class that implements the interface IF_OO_ADT_CLASSRUN (suggested name: ZCL_##_COMPUTE, where ## stands for your group number).
Choose File→New→ABAP Class.
Enter the name of your package in the Package field. In the Name field, enter the name ZCL_##_COMPUTE, where ## stands for your group number.
Enter a description.
In the Interfaces group box, choose Add.
Enter IF_OO_ADT_CLASSRUN. When the interface appears in the hit list, double-click it to add it to the class definition.
Choose Next.
Select your transport request and choose Finish.
In the IF_OO_ADT_CLASSRUN~MAIN method, declare two variables for integer values (suggested names: number1 and number2).
Add the following code:
Code Snippet
Copy code
Switch to dark mode
DATA number1 TYPE i.
DATA number2 TYPE i.
Implement a value assignment for each of the two numeric variables. Assign a negative value to the first variable and a positive value to the second variable.
Add the following code:
Code Snippet
Copy code
Switch to dark mode
number1 = -8.
number2 = 3.
Task 2: Data Processing
Calculate a ratio and write the result to the console.
Steps
Implement an arithmetic expression that calculates the ratio of the two numbers. Store the result in a variable that you declare using an inline declaration (suggested name: result).
Add the following code:
Code Snippet
Copy code
Switch to dark mode
DATA(result) = number1 / number2.
Implement a string template that puts together a text like this:"6 / 3 = 2", with the actual values of the variables instead of 6, 3, and 2. Store the result in a variable that you declare using an inline declaration (suggested name: output).
Add the following code:
Code Snippet
Copy code
Switch to dark mode
DATA(output) = |{ number1 } / { number2 } = { result }|.
Call method out->write( … ) to write the text to the console.
Proceed as you have done in the previous exercise.
Task 3: Activate and Test
Activate and test the console app.
Steps
Activate the class.
Press Ctrl + F3 to activate the class.
Execute the console app.
Press F9 to execute the console app.
Test the app with different input values.
Edit the value assignments, then activate and execute as before.
Task 4: Control the Result Precision
Make sure the result is rounded to a value with 2 decimal places, and not to an integer value.
Steps
Instead of an inline declaration, use an explicit declaration of variable result. Use a numeric type that allows to specify the number of decimal places.
At the beginning of method if_oo_adt_classrun~main, add the following code:
Code Snippet
Copy code
Switch to dark mode
DATA result TYPE p LENGTH 8 DECIMALS 2.
In the code line with the inline declaration, replace DATA(result) with result.
Activate the class and test again.
Proceed as you have done before.
Compare your code to the following extract from the model solution:
Code Snippet
Copy code
Switch to dark mode
METHOD if_oo_adt_classrun~main.
* Declarations
**************************
DATA number1 TYPE i.
DATA number2 TYPE i.
DATA result TYPE p LENGTH 8 DECIMALS 2.
* Input Values
**************************
number1 = -8.
number2 = 3.
* Calculation
**************************
* DATA(result) = number1 / number2. "with inline declaration
result = number1 / number2.
DATA(output) = |{ number1 } / { number2 } = { result }|.
* Output
**************************
out->write( output ).
ENDMETHOD.
Objectives
After completing this lesson, you will be able to:
Perform arithmetic calculations.
Apply string processing.
Arithmetic Calculations
Arithmetic expressions in ABAP combine values, operators, and functions to compute a result. The data type of the result is determined by the types of the operands used in the expression. These expressions can be used anywhere a reading operand is expected, such as the right-hand side of a value assignment.
Operators and Functions
ABAP supports standard arithmetic operators:
Addition:
+(e.g.,result = 5 + 3.)Subtraction:
-(e.g.,result = 5 - 3.)Multiplication:
*(e.g.,result = 5 * 3.)Division:
/(e.g.,result = 5 / 3.)Modulo:
MOD(returns the remainder of a division, e.g.,result = 5 MOD 3.which yields 2).
Additionally, ABAP provides built-in functions for more complex operations:
sqrt( argument ): Calculates the square root of theargument. For instance,result = sqrt( 9 ).would assign 3 toresult.ipow( base = <base_value> exp = <exponent_value> ): Calculates thebase_valueraised to the power ofexponent_value. Bothbaseandexpmust be numeric. For example,result = ipow( base = 2 exp = 3 ).computes 2^3 = 8. Note thatexpmust be an integer.
Operator Precedence
Expressions are evaluated according to standard mathematical precedence:
Parentheses
(): Expressions inside parentheses are evaluated first.Multiplication
*, Division/, ModuloMOD.Addition
+, Subtraction-.
For example, ( 8 * 7 - 6 ) / ( 5 + 4 ) evaluates to ( 56 - 6 ) / 9 which is 50 / 9. In contrast, 8 * 7 - 6 / 5 + 4 evaluates as (8 imes 7) - (6 / 5) + 4 = 56 - 1.2 + 4 = 58.8.
Result Type Dependency
The result type of an arithmetic expression significantly depends on the type defined for the result variable. Consider the division 2 / 3:
If
resultisTYPE i(integer), the result will be truncated to0(performing integer division).If
resultisTYPE p LENGTH 8 DECIMALS 0(packed number with no decimals), the result will be1(rounded after internal calculation).If
resultisTYPE p LENGTH 8 DECIMALS 2(packed number with 2 decimal places), the result will be0.67(rounded correctly to two decimal places).
Try It Out: Arithmetic Calculations
As in the first exercise, create a new global class that implements interface IF_OO_ADT_CLASSRUN. Copy the following code snippet into the implementation part of method if_oo_adt_classrun~main():
* Declarations
TYPES t_result TYPE p LENGTH 8 DECIMALS 2.
* TYPES t_result TYPE p LENGTH 8 DECIMALS 0.
* TYPES t_result TYPE i.
DATA result TYPE t_result.
* Calculations
result = 2 + 3.
* result = 2 - 3.
* result = 2 * 3.
* result = 2 / 3.
* result = sqrt( 2 ).
* result = ipow( base = 2 exp = 3 ).
* result = ( 8 * 7 - 6 ) / ( 5 + 4 ).
* result = 8 * 7 - 6 / 5 + 4.
* Output
out->write( result ).
Press CTRL + F3 to activate the class and F9 to execute it as a console app. Experiment with the source code by uncommenting different calculations and type definitions, and implement your own calculations to observe the effects.
String Processing
String templates are ABAP expressions that produce a result of type string. They can be used in any reading operand position, such as assigning a value to a variable. A string template is delineated by pipe symbols (|) at its beginning and end (e.g., |Hello World|).
The key feature distinguishing string templates from simple string literals is their ability to embed expressions. An embedded expression is any ABAP expression enclosed within curly brackets ({ and }).
DATA(text) = |Total: { amount } EUR|.
At runtime, ABAP evaluates the embedded expression and converts its result into a string, which then replaces the embedded expression (including its curly brackets) in the final string template. ABAP syntax requires at least one blank space after the opening { and before the closing } bracket for embedded expressions. A single string template can contain multiple embedded expressions.
Types of Embedded Expressions
Inside the curly brackets, you can place various types of ABAP expressions:
Variables:
|My name is { my_name }|Literals:
|The number is { 123 }|Arithmetic Expressions:
|Sum: { amount1 + amount2 }|System Fields or Function Calls:
|Current time: { sy-timlo }|
Formatting Data with String Templates
One significant use case for string templates is the controlled formatting of data for output. This is achieved by adding format options within the curly brackets of an embedded expression.
Date Formatting (DATE = <date_format>)
When a variable of type d (date) is embedded, it typically outputs in its internal YYYYMMDD format (e.g., 19891109). However, you can apply format options:
DATE = ISO: Formats the date to theYYYY-MM-DDstandard. Example:{ the_date DATE = ISO }for19891109would output1989-11-09.DATE = USER: Formats the date according to the current user's personalized settings. Example:{ the_date DATE = USER }for19891109might output09.11.1989or11/09/1989depending on user locale settings.DATE = ALPHA: Formats the date asDD.MM.YYYY. Example:{ the_date DATE = ALPHA }for19891109would output09.11.1989.
Number Formatting (NUMBER = <number_format>)
This controls the general formatting of numbers, such as decimal and thousands separators.
NUMBER = USER: Formats the number based on the user's settings (e.g., comma for decimal point in some locales, period in others). Example:{ my_number NUMBER = USER }for'-273.15'might output'-273,15'in a German locale or'-273.15'in an English locale.NUMBER = RAW: Outputs the number without any special formatting, using a period as the decimal separator and no thousands separator.
Sign Formatting (SIGN = <sign_pos>)
Controls the position and display of the sign for numeric values.
SIGN = RAW: Default behavior, sign prefix (e.g.,-123.45).SIGN = RIGHT: Places the sign after the number (e.g.,123.45-).SIGN = SPACE: For positive numbers, a space replaces the+sign (e.g.,123.45). Negative numbers retain their-sign.SIGN = PLUS: Explicitly displays a+sign for positive numbers (e.g.,+123.45).
Example for my_number = '-273.15':
{ my_number SIGN = RIGHT }output:273.15-
Style Formatting (STYLE = <style_type>)
Applies predefined styles to numbers.
STYLE = SCIENTIFIC: Formats the number in scientific notation (e.g.,my_number = '-273.15'would become$-2.7315 imes 10^2$).STYLE = ENGINEERING: Formats numbers using engineering notation, typically with exponents divisible by three.
String Concatenation (&&)
Data objects and string expressions can be joined using the concatenation operator &&. This operator joins the constituent parts directly, without any spaces or separators in between. If spaces or other separator characters are required, they must be explicitly inserted as part of the expression.
Example:
part1 && part2(wherepart1 = 'Hello'andpart2 = 'World') would result in'HelloWorld'.part1 && | | && part2would result in'Hello World'.|{ amount1 } + { amount2 }| && | = | && |{ amount1 + amount2 }|would concisely output a formatted calculation like'1.20 + 2.10 = 3.30'.
Try It Out: String Processing
Create a new global class that implements interface IF_OO_ADT_CLASSRUN. Copy the following code snippet into the implementation part of method if_oo_adt_classrun~main():
METHOD if_oo_adt_classrun~main.
* Declarations
TYPES t_amount TYPE p LENGTH 8 DECIMALS 2.
DATA amount TYPE t_amount VALUE '3.30'.
DATA amount1 TYPE t_amount VALUE '1.20'.
DATA amount2 TYPE t_amount VALUE '2.10'.
DATA the_date TYPE d VALUE '19891109'.
DATA my_number TYPE p LENGTH 3 DECIMALS 2 VALUE '-273.15'.
DATA part1 TYPE string VALUE `Hello`.
DATA part2 TYPE string VALUE `World`.
* String Templates
DATA(text) = |Hello World|.
* DATA(text) = |Total: { amount } EUR|.
* DATA(text) = |Total: { amount1 + amount2 } EUR|.
* Format Options
"Date
* DATA(text) = |Raw Date: { the_date }|.
* DATA(text) = |ISO Date: { the_date Date = ISO }|.
* DATA(text) = |USER Date: { the_date Date = USER }|.
"Number
* DATA(text) = |Raw Number { my_number }|.
* DATA(text) = |User Format { my_number NUMBER = USER }|.
* DATA(text) = |Sign Right { my_number SIGN = RIGHT }|.
* DATA(text) = |Scientific { my_number STYLE = SCIENTIFIC }|.
* String expression (concatenation Operator)
* DATA(text) = part1 && part2.
* DATA(text) = part1 && | | && part2.
* DATA(text) = |{ amount1 } + { amount2 }| &&
* | = | &&
* |{ amount1 + amount2 }|.
* Output
out->write( text ).
ENDMETHOD.
Press CTRL + F3 to activate the class and F9 to execute it. Uncomment different assignments for the text variable to observe various formatting results. You can also declare your own variables and experiment with different format options and concatenation scenarios.
Declare Variables and Process Data
In this exercise, you will create a program that performs a calculation based on two numbers, then formats and outputs the result.
Task 1: Variable Declaration
In your own package, create a new global class and define numeric variables for the input.
Create a new global class: Implement the interface
IF_OO_ADT_CLASSRUN(suggested name:ZCL_##_COMPUTE, where##is your group number).Choose
File->New->ABAP Class.Enter your package name. In the
Namefield, enterZCL_##_COMPUTE.Add a description.
In the
Interfacesgroup box, chooseAddand enterIF_OO_ADT_CLASSRUN. Double-click it from the hit list.Choose
Next, select your transport request, andFinish.
Declare integer variables: In the
IF_OO_ADT_CLASSRUN~MAINmethod, declare two integer variables (e.g.,number1andnumber2).DATA number1 TYPE i. DATA number2 TYPE i.Assign values: Implement a value assignment for each variable, assigning a negative value to
number1and a positive value tonumber2.number1 = -8. number2 = 3.
Task 2: Data Processing
Calculate a ratio and write the result to the console.
Calculate the ratio: Implement an arithmetic expression to calculate the ratio of the two numbers. Store the result in a variable declared using an inline declaration (suggested name:
result).DATA(result) = number1 / number2.Create a formatted string: Implement a string template to create a text like "6 / 3 = 2", but with the actual values of your variables. Store this in an inline-declared variable
output.DATA(output) = |{ number1 } / { number2 } = { result }|.Output to console: Call method
out->write( ... )to display theoutputstring to the console.out->write( output ).
Task 3: Activate and Test
Activate and test your console application.
Activate the class: Press
Ctrl + F3.Execute the console app: Press
F9.Test with different inputs: Edit the assigned values in your code, then activate and execute again.
Task 4: Control the Result Precision
Ensure the division result is rounded to two decimal places, rather than an integer.
Explicit declaration for
result: Instead of an inline declaration, explicitly declareresultusing a numeric type that allows specifying the number of decimal places. Add the following code at the beginning ofif_oo_adt_classrun~main:DATA result TYPE p LENGTH 8 DECIMALS 2.Modify assignment: Replace
DATA(result)withresultin the calculation line.result = number1 / number2.Activate and test again: Observe the change in precision.
Compare your completed code to the following model solution extract:
```ABAP
METHOD ifooadt_classrun~main.
Declarations
DATA
Objectives
After completing this lesson, you will be able to:
Perform arithmetic calculations.
Apply string processing.
Arithmetic Calculations
Arithmetic expressions in ABAP combine values, operators, and functions to compute a result. The data type of the result is determined by the types of the operands used in the expression. These expressions can be used anywhere a reading operand is expected, such as the right-hand side of a value assignment.
Operators and Functions
ABAP supports standard arithmetic operators:
Addition:
+(e.g.,result = 5 + 3.)Subtraction:
-(e.g.,result = 5 - 3.)Multiplication:
*(e.g.,result = 5 * 3.)Division:
/(e.g.,result = 5 / 3.)Modulo:
MOD(returns the remainder of a division, e.g.,result = 5 MOD 3.which yields 2).
Additionally, ABAP provides built-in functions for more complex operations:
sqrt( argument ): Calculates the square root of theargument. For instance,result = sqrt( 9 ).would assign 3 toresult.ipow( base = <base_value> exp = <exponent_value> ): Calculates thebase_valueraised to the power ofexponent_value. Bothbaseandexpmust be numeric. For example,result = ipow( base = 2 exp = 3 ).computes 2^3 = 8. Note thatexpmust be an integer.
Operator Precedence
Expressions are evaluated according to standard mathematical precedence:
Parentheses
(): Expressions inside parentheses are evaluated first.Multiplication
*, Division/, ModuloMOD.Addition
+, Subtraction-.
For example, ( 8 * 7 - 6 ) / ( 5 + 4 ) evaluates to ( 56 - 6 ) / 9 which is 50 / 9. In contrast, 8 * 7 - 6 / 5 + 4 evaluates as (8 \times 7) - (6 / 5) + 4 = 56 - 1.2 + 4 = 58.8.
Result Type Dependency
The result type of an arithmetic expression significantly depends on the type defined for the result variable. Consider the division 2 / 3:
If
resultisTYPE i(integer), the result will be truncated to0(performing integer division).If
resultisTYPE p LENGTH 8 DECIMALS 0(packed number with no decimals), the result will be1(rounded after internal calculation).If
resultisTYPE p LENGTH 8 DECIMALS 2(packed number with 2 decimal places), the result will be0.67(rounded correctly to two decimal places).
Try It Out: Arithmetic Calculations
As in the first exercise, create a new global class that implements interface IF_OO_ADT_CLASSRUN. Copy the following code snippet into the implementation part of method if_oo_adt_classrun~main():
* Declarations
TYPES t_result TYPE p LENGTH 8 DECIMALS 2.
* TYPES t_result TYPE p LENGTH 8 DECIMALS 0.
* TYPES t_result TYPE i.
DATA result TYPE t_result.
* Calculations
result = 2 + 3.
* result = 2 - 3.
* result = 2 * 3.
* result = 2 / 3.
* result = sqrt( 2 ).
* result = ipow( base = 2 exp = 3 ).
* result = ( 8 * 7 - 6 ) / ( 5 + 4 ).
* result = 8 * 7 - 6 / 5 + 4.
* Output
out->write( result ).
Press CTRL + F3 to activate the class and F9 to execute it as a console app. Experiment with the source code by uncommenting different calculations and type definitions, and implement your own calculations to observe the effects.
String Processing
String templates are ABAP expressions that produce a result of type string. They can be used in any reading operand position, such as assigning a value to a variable. A string template is delineated by pipe symbols (|) at its beginning and end (e.g., |Hello World|).
The key feature distinguishing string templates from simple string literals is their ability to embed expressions. An embedded expression is any ABAP expression enclosed within curly brackets ({ and }).
DATA(text) = |Total: { amount } EUR|.
At runtime, ABAP evaluates the embedded expression and converts its result into a string, which then replaces the embedded expression (including its curly brackets) in the final string template. ABAP syntax requires at least one blank space after the opening { and before the closing } bracket for embedded expressions. A single string template can contain multiple embedded expressions.
Types of Embedded Expressions
Inside the curly brackets, you can place various types of ABAP expressions:
Variables:
|My name is { my_name }|Literals:
|The number is { 123 }|Arithmetic Expressions:
|Sum: { amount1 + amount2 }|System Fields or Function Calls:
|Current time: { sy-timlo }|
Formatting Data with String Templates
One significant use case for string templates is the controlled formatting of data for output. This is achieved by adding format options within the curly brackets of an embedded expression.
Detailed Formatting Options
Below are detailed comparisons of various format options available within ABAP string templates.
Date Formatting (DATE = <date_format>)
This option controls how date variables (type d) are displayed. The default output is YYYYMMDD (internal format).
| Description | Example (for | Output |
|---|---|---|---|
| Formats the date to the |
|
|
| Formats the date according to the current user's personalized settings (locale-dependent). |
|
|
| Formats the date as |
|
|
Number Formatting (NUMBER = <number_format>)
This option controls the general formatting of numbers, including decimal and thousands separators.
| Description | Example (for | Output |
|---|---|---|---|
| Formats the number based on the user's locale settings. |
|
|
| Outputs the number without special formatting, using a period as the decimal separator and no thousands separator. |
|
|
Sign Formatting (SIGN = <sign_pos>)
This option controls the position and display of the sign for numeric values.
| Description | Example (for | Output | Example (for |
|---|---|---|---|---|
| Default behavior; sign is prefixed. |
|
|
|
| Places the sign after the number. |
|
|
|
| For positive numbers, a space replaces the |
|
|
|
| Explicitly displays a |
|
|
|
Style Formatting (STYLE = <style_type>)
This option applies predefined scientific or engineering styles to numbers.
| Description | Example (for | Output |
|---|---|---|---|
| Formats the number in scientific notation. |
|
|
| Formats numbers using engineering notation (exponents divisible by three). |
|
|
String Concatenation (&&)
Data objects and string expressions can be joined using the concatenation operator &&. This operator joins the constituent parts directly, without any spaces or separators in between. If spaces or other separator characters are required, they must be explicitly inserted as part of the expression.
Example:
part1 && part2(wherepart1 = 'Hello'andpart2 = 'World') would result in'HelloWorld'.part1 && | | && part2would result in'Hello World'.|{ amount1 } + { amount2 }| && | = | && |{ amount1 + amount2 }|would concisely output a formatted calculation like'1.20 + 2.10 = 3.30'.
Try It Out: String Processing
Create a new global class that implements interface IF_OO_ADT_CLASSRUN. Copy the following code snippet into the implementation part of method if_oo_adt_classrun~main():
METHOD if_oo_adt_classrun~main.
* Declarations
TYPES t_amount TYPE p LENGTH 8 DECIMALS 2.
DATA amount TYPE t_amount VALUE '3.30'.
DATA amount1 TYPE t_amount VALUE '1.20'.
DATA amount2 TYPE t_amount VALUE '2.10'.
DATA the_date TYPE d VALUE '19891109'.
DATA my_number TYPE p LENGTH 3 DECIMALS 2 VALUE '-273.15'.
DATA part1 TYPE string VALUE `Hello`.
DATA part2 TYPE string VALUE `World`.
* String Templates
DATA(text) = |Hello World|.
* DATA(text) = |Total: { amount } EUR|.
* DATA(text) = |Total: { amount1 + amount2 } EUR|.
* Format Options
"Date
* DATA(text) = |Raw Date: { the_date }|.
* DATA(text) = |ISO Date: { the_date Date = ISO }|.
* DATA(text) = |USER Date: { the_date Date = USER }|.
"Number
* DATA(text) = |Raw Number { my_number }|.
* DATA(text) = |User Format { my_number NUMBER = USER }|.
* DATA(text) = |Sign Right { my_number SIGN = RIGHT }|.
* DATA(text) = |Scientific { my_number STYLE = SCIENTIFIC }|.
* String expression (concatenation Operator)
* DATA(text) = part1 && part2.
* DATA(text) = part1 && | | && part2.
* DATA(text) = |{ amount1 } + { amount2 }| &&
* | = | &&
* |{ amount1 + amount2 }|.
* Output
out->write( text ).
ENDMETHOD.
Press CTRL + F3 to activate the class and F9 to execute it. Uncomment different assignments for the text variable to observe various formatting results. You can also declare your own variables and experiment with different format options and concatenation scenarios.
Declare Variables and Process Data
In this exercise, you will create a program that performs a calculation based on two numbers, then formats and outputs the result.
Task 1: Variable Declaration
In your own package, create a new global class and define numeric variables for the input.
Create a new global class: Implement the interface
IF_OO_ADT_CLASSRUN(suggested name:ZCL_##_COMPUTE, where##is your group number).Choose
File->New->ABAP Class.Enter your package name. In the
Namefield, enterZCL_##_COMPUTE.Add a description.
In the
Interfacesgroup box, chooseAddand enterIF_OO_ADT_CLASSRUN. Double-click it from the hit list.Choose
Next, select your transport request, andFinish.
Declare integer variables: In the
IF_OO_ADT_CLASSRUN~MAINmethod, declare two integer variables (e.g.,number1andnumber2).DATA number1 TYPE i. DATA number2 TYPE i.Assign values: Implement a value assignment for each variable, assigning a negative value to
number1and a positive value tonumber2.number1 = -8. number2 = 3.
Task 2: Data Processing
Calculate a ratio and write the result to the console.
Calculate the ratio: Implement an arithmetic expression to calculate the ratio of the two numbers. Store the result in a variable declared using an inline declaration (suggested name:
result).DATA(result) = number1 / number2.Create a formatted string: Implement a string template to create a text like "6 / 3 = 2", but with the actual values of your variables. Store this in an inline-declared variable
output.DATA(output) = |{ number1 } / { number2 } = { result }|.Output to console: Call method
out->write( ... )to display theoutputstring to the console.out->write( output ).
Task 3: Activate and Test
Activate and test your console application.
Activate the class: Press
Ctrl + F3.Execute the console app: Press
F9.Test with different inputs: Edit the assigned values in your code, then activate and execute again.
Task 4: Control the Result Precision
Ensure the division result is rounded to two decimal places, rather than an integer.
Explicit declaration for
result: Instead of an inline declaration, explicitly declareresultusing a numeric type that allows specifying the number of decimal places. Add the following code at the beginning ofif_oo_adt_classrun~main:DATA result TYPE p LENGTH 8 DECIMALS 2.Modify assignment: Replace
DATA(result)withresultin the calculation line.result = number1 / number2.Activate and test again: Observe the change in precision.
Compare your completed code to the following model solution extract:
METHOD if_oo_adt_classrun~main.
* Declarations
DATA