Processing Data

Objectives
After completing this lesson, you will be able to:

After completing this lesson, you will be able to:

  • Perform arithmetic calculations
  • Apply string processing

Arithmetic Calculations

Arithmetic Expressions

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

  1. Like in the first exercise of this course, create a new global class that implements interface IF_OO_ADT_CLASSRUN.
  2. Copy the following code snippet to the implementation part of method if_oo_adt_classrun~main( ):
    Code snippet
    
      * 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 ).
    
    Copy code
  3. Press CTRL + F3 to activate the class and F9 to execute it as a console app.
  4. Play around with the source code to get familiar with the concepts.
    • Uncomment different calculations and type definitions.
    • Implement your own calculations.

Processing Strings

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.

Let's see how can process strings using ABAP.

Try It Out: String Processing

  1. Like in the first exercise of this course, create a new global class that implements interface IF_OO_ADT_CLASSRUN.
  2. Copy the following code snippet to the implementation part of method if_oo_adt_classrun~main( ):
    Code snippet
    
     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_numer                    }|.
    *    DATA(text) = |User Format{ my_numer 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 ).
    
    Copy code
  3. Press CTRL + F3 to activate the class and F9 to execute it as a console app.
  4. 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.

Log in to track your progress & complete quizzes