ABAP Core
ABAP Developmentbeginner

ABAP Core Part 7: Operators, Calculations and Type Conversion

Key takeaway

Calculate and convert correctly in classic ABAP: arithmetic with DIV and MOD, rounding rules, packed numbers for money, ROUND and CEIL/FLOOR functions, string statements (CONDENSE, TRANSLATE, SEARCH, SPLIT, REPLACE), MOVE conversions, conversion exits and catching conversion errors.

Explanation

📌 KEY POINTS
✅ Arithmetic: + - * / plus DIV (integer division), MOD (remainder) and (power).

✅ The result type follows the target field: assigning 7 / 2 to an integer gives 4 (rounded), not 3.

✅ String handling statements: CONCATENATE, SPLIT, CONDENSE, TRANSLATE, REPLACE, SEARCH / FIND, plus strlen( ).

⚠️ Moving non-numeric text into a number raises CX_SY_CONVERSION_NO_NUMBER — validate input or catch it.

━━━━━━━━━━━━━━━━━━━━
▶ WHERE WE ARE

Part 3 introduced types. Now we combine values: calculations, text processing and conversions.

━━━━━━━━━━━━━━━━━━━━
▶ 1. ARITHMETIC

lv_total = lv_price * lv_seats.

  • / normal division; DIV integer quotient; MOD remainder (7 DIV 2 = 3, 7 MOD 2 = 1).
  • ADD 1 TO lv_x and lv_x = lv_x + 1 are equivalent.
  • Integer targets round commercially; packed targets keep the declared decimals.

━━━━━━━━━━━━━━━━━━━━
▶ 2. MONEY AND ROUNDING

  • Use TYPE p (packed) with DECIMALS for amounts — never float.
  • Intermediate results are calculated with high precision, then rounded to the target's decimals.
  • Functions: round( val = x dec = 2 ), ceil( x ), floor( x ), abs( x ).

━━━━━━━━━━━━━━━━━━━━
▶ 3. TEXT PROCESSING

  • CONCATENATE a b INTO c SEPARATED BY space.
  • SPLIT lv_line AT ';' INTO lv_f1 lv_f2 lv_f3 (or INTO TABLE lt_parts).
  • CONDENSE lv_text (NO-GAPS removes all blanks).
  • TRANSLATE lv_text TO UPPER CASE.
  • REPLACE ALL OCCURRENCES OF ',' IN lv_num WITH '.'.
  • SEARCH / FIND 'CARGO' IN lv_text — sy-subrc 0 if found.

━━━━━━━━━━━━━━━━━━━━
▶ 4. CONVERSIONS

  • MOVE / = converts between compatible types automatically (e.g. c → n, c → p when the text is numeric).
  • Conversion exits: CONVERSION_EXIT_ALPHA_INPUT / _OUTPUT for leading zeros; WRITE … TO for formatted output (dates, amounts with CURRENCY).
  • Invalid numbers: catch CX_SY_CONVERSION_NO_NUMBER, or check with CO '0123456789 ' / CATCH SYSTEM-EXCEPTIONS in very old code.

━━━━━━━━━━━━━━━━━━━━
▶ 5. MODERN SYNTAX (SIDE NOTE)

From 7.40: CONV type( ), EXACT type( ), string functions like to_upper( ), condense( ) and embedded templates |{ … }|. Same results, more compact.

━━━━━━━━━━━━━━━━━━━━
▶ STEP BY STEP

  1. Compare 7 / 2 in an integer and in a packed field.
  2. Sum three packed prices and round a share to 2 decimals.
  3. Split a CSV line, clean the fields and convert the amount with error handling.

✅ CHECKPOINT: you can calculate, process texts and convert values safely with classic statements.

Code example

ABAP Code
REPORT zabap_core_expressions. DATA: lv_i       TYPE i,      lv_div     TYPE i,      lv_mod     TYPE i,      lv_price   TYPE p LENGTH 8 DECIMALS 2 VALUE '199.99',      lv_share   TYPE p LENGTH 16 DECIMALS 6,      lv_rounded TYPE p LENGTH 8 DECIMALS 2,      lv_line    TYPE string VALUE 'LH;0400;  1.234,50 ',      lv_carrid  TYPE string,      lv_connid  TYPE string,      lv_amount  TYPE string,      lv_num     TYPE p LENGTH 10 DECIMALS 2,      lv_text    TYPE string VALUE '  Lufthansa Cargo  ',      lv_conn    TYPE s_conn_id,      lv_conn_ext TYPE c LENGTH 4,      lx_conv    TYPE REF TO cx_sy_conversion_no_number,      lv_msg     TYPE string. START-OF-SELECTION.* ===== Arithmetic and integer rounding =====  lv_i = 7 / 2.                                  " 4 (rounded), NOT 3  lv_div = 7 DIV 2.                              " 3  lv_mod = 7 MOD 2.                              " 1  WRITE: / '7 / 2 into i =', lv_i, 'DIV:', lv_div, 'MOD:', lv_mod. * ===== Money: packed numbers and explicit rounding =====  lv_share   = lv_price / 3.  lv_rounded = round( val = lv_share dec = 2 ).  WRITE: / 'price / 3 =', lv_share, 'rounded:', lv_rounded. * ===== Text processing =====  CONDENSE lv_text.  TRANSLATE lv_text TO UPPER CASE.               " 'LUFTHANSA CARGO'  SEARCH lv_text FOR 'CARGO'.  IF sy-subrc = 0.    WRITE: / lv_text, 'contains CARGO'.  ENDIF.   SPLIT lv_line AT ';' INTO lv_carrid lv_connid lv_amount.  CONDENSE lv_amount NO-GAPS.                    " '1.234,50'  REPLACE ALL OCCURRENCES OF '.' IN lv_amount WITH ''.  REPLACE ALL OCCURRENCES OF ',' IN lv_amount WITH '.'.   " '1234.50' * ===== Conversion with error handling =====  TRY.      lv_num = lv_amount.      WRITE: / 'Amount:', lv_num.    CATCH cx_sy_conversion_no_number INTO lx_conv.      lv_msg = lx_conv->get_text( ).      WRITE: / 'Not a number:', lv_msg.  ENDTRY. * ===== Conversion exit for leading zeros =====  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'    EXPORTING input  = '400'    IMPORTING output = lv_conn.                  " '0400' internal format  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'    EXPORTING input  = lv_conn    IMPORTING output = lv_conn_ext.              " '400' external format  WRITE: / 'internal:', lv_conn, 'external:', lv_conn_ext. * 💡 Modern syntax (7.40+): CONV i( ... ), EXACT i( ... ), to_upper( condense( ... ) ),*    |{ lv_conn ALPHA = OUT }| do the same in expressions.

Real project scenario

An interface read material and connection numbers from a CSV file and selected from the database without conversion. Records with "400" instead of "0400" were not found, and the interface reported them as missing master data every night.
Adding ALPHA = IN conversion (and the correct conversion exits for material numbers) before the database read solved it. The team also used EXACT for numeric fields from the file so that malformed values raised clear errors instead of silently truncating.

Common mistakes

  • Expecting integer division to truncate (it rounds).
  • Using f or i for amounts.
  • Ignoring conversion exits (leading zeros) for keys from files.
  • Silent truncation when assigning longer text to shorter fields.
  • Not catching conversion exceptions for external input.

Best practices

  • Choose the target type deliberately; round explicitly.
  • Use DIV/MOD for integer logic.
  • Use built-in functions instead of manual string handling.
  • Use EXACT for external data where loss is unacceptable.
  • Apply conversion exits (ALPHA etc.) for keys from outside SAP.

Interview angle

Questions: "What is 7 / 2 in an integer?", "Difference between CONV and EXACT?", "Why is ALPHA conversion needed?"
Strong answer: integer targets round (7 / 2 = 4), DIV gives 3 and MOD 1; CONV converts explicitly, EXACT converts only without data loss and raises CX_SY_CONVERSION_ERROR otherwise; conversion exits like ALPHA translate between external and internal formats, e.g. adding leading zeros before database reads.

Practice this lesson

Quick check

1. What is the result of lv_i = 7 / 2 when lv_i is of type i?

2. Which type is correct for a currency amount?

3. When using MOVE-CORRESPONDING to copy data between two structures that have differently ordered components with the same names but possibly different types, what happens?

Interview questions

1. Amount fields show wrong values after arithmetic. What went wrong?

Answer it in your own words first, then compare.

2. An inbound interface compares customer number '123' with '0000000123' in the database and finds no match. What is the fix?

Answer it in your own words first, then compare.

Practice more with Interview AI →

Reference-verified: the steps are checked against official SAP documentation and tutorials, and object and field names in the sample code are checked for consistency. The code has not been compiled or run on an SAP system by ERPClimb. Syntax marked release-dependent varies by SAP release, so check it in your own system.

More lessons in this topic

Interview questions on this topic

Related SAP topics