Start of Content Area

Arithmetic Calculations  Locate the document in its SAP Library structure

Arithmetic calculations use arithmetic operations and special ABAP statements.

Basic Arithmetic Operations

ABAP supports the four basic arithmetic operations and power caluclations. You can use the following arithmetic operators in mathematical expressions:

Operator

Meaning

+

Addition

‑{}‑-

Subtraction

‑{}‑*

Multiplication

‑{}‑/

Division

DIV

Integer division

MOD

Remainder of integer division

‑{}‑**

Powers

Instead of using operators in mathematical expressions, you can perform basic arithmetic operations with the keywords ADD, SUBTRACT, MULTIPLY, and DIVIDE.

The following table shows the different ways of expressing basic arithmetic operations in ABAP:

Operation

 

Statement using

mathematical expression

Statement using

Keyword

Addition

 p = n + m.

ADD n TO m.

Subtraction

 P = m – n.

SUBTRACT n FROM m.

Multiplication

 P = m * n.

MULTIPLY m BY n.

Division

 P = m / n.

DIVIDE m BY n.

Integer division

 P = m DIV n.

---

Remainder of division

 P = m MOD n.

---

Powers

 P = m ** n.

---

In the statements using keywords, the results of the operations are assigned to field <m>.

The operands  m, n, p can be any numeric fields. If the fields are not of the same data type, the system converts all fields into the hierarchically highest data type that occurs in the statement.

When using mathematical expressions, please note that the operators +, -, *, ** and /, as well as opening and closing parentheses, are interpreted as words in ABAP and must therefore be preceded and followed by blanks.

In division operations, the divisor cannot be zero if the dividend is not zero. With integer division, you use the operators DIV or MOD instead of /. DIV calculates the integer quotient, MOD calculates the remainder.

If you combine several arithmetic expressions together, calculations are performed from left to right for operators of equal priority, except in the case of exponentiation which is performed from right to left. So n ** m ** p is the same as n ** ( m ** p ), not
( n ** m ) ** p

Example

DATA: counter TYPE i.

COMPUTE counter = counter + 1.

counter = counter + 1.

ADD 1 TO counter.

Here, the three operational statements perform the same arithmetic operation, i.e. adding 1 to the contents of the field counterand assigning the result to counter.

Example

DATA: pack TYPE p DECIMALS 4,
      n TYPE f VALUE '+5.2',
      m TYPE f VALUE '+1.1'.

pack = n / m.
WRITE pack.

pack = n DIV m.
WRITE  / pack.

pack = n MOD m.
WRITE / pack.

The output appears as follows:

           4.7273

           4.0000

           0.8000

This example shows the different types of division.

Arithmetic Calculations Using Structures

CautionThese statements are obsolete and are only available to ensure compatibility with Releases prior to 4.6 and 6.10. The statements may appear in older programs but should no longer be used.

 

In the same way that you can transfer values component by component between structures using the MOVE-CORRESPONDING statement, you can also perform arithmetic operations between the components of structures using the following statements:

·        ADD-CORRESPONDING

·        SUBTRACT-CORRESPONDING

·        MULTIPLY-CORRESPONDING

·        DIVIDE-CORRESPONDING

ABAP performs the corresponding calculation for all components that have the same name in both structures. However, it only makes sense to use the operations if all of the components involved have a numeric data type.

Example

DATA: BEGIN OF rate,

         usa TYPE f VALUE '0.6667',

         frg TYPE f VALUE '1.0',

         aut TYPE f VALUE '7.0',

      END OF rate.

 

DATA: BEGIN OF money,
         usa TYPE i VALUE 100,
         frg TYPE i VALUE 200,
         aut TYPE i VALUE 300,
      END OF money.

MULTIPLY-CORRESPONDING money BY rate.

WRITE / money-usa.
WRITE / money-frg.
WRITE / money-aut.

The output appears as follows:

        67

       200

     2,100

Here, money-usa is multiplied by rate-usa and so on.

Adding Sequences of Fields

CautionThis statement is obsolete and is only available to ensure compatibility with Releases prior to 4.6 and 6.10. The statement may appear in older programs but should no longer be used.

 

There are variants of the ADDstatement that allow you to add sequences of fields in memory. For example:

Adding sequences of fields and assigning the result to another field

ADD n1 THEN n2 UNTIL nz GIVING m.

If n1, n2, ... , nz is a sequence of equidistant fields of the same type and length in memory, they are summed and the result is assigned to m.

Adding sequences of fields and adding the result to the contents of another field

ADD n1 THEN n2 UNTIL nz TO m.

This statement works exactly like the previous one, but with the exception that the sum of the field values is added to the old contents of m.

For further information about other similar variants, see the keyword documentation for the ADD statement.

Example

DATA: BEGIN OF series,
         n1 TYPE i VALUE 10,
         n2 TYPE i VALUE 20,
         n3 TYPE i VALUE 30,
         n4 TYPE i VALUE 40,
         n5 TYPE i VALUE 50,
         n6 TYPE i VALUE 60,
      END OF series.

DATA sum TYPE i.

ADD series-n1 THEN series-n2 UNTIL series-n5 GIVING sum.
WRITE sum.

ADD series-n2 THEN series-n3 UNTIL series-n6 TO sum.
WRITE / sum.

The output is as follows:

       150

       350

Here, the contents of components n1 to n5 are summed and assigned to the field sum. Then, the contents of components n2 to n6 are summed and added to the value of sum.

 

 

End of Content Area