ABAP Core Part 7: Operators, Calculations and Type Conversion
Lessons are open to try during our launch period. Some advanced content may move to Pro later. AI practice limits apply separately.
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.
In this lessonShow sectionsHide sections
Visual overview
Expert visual walkthrough
Operators, Calculations and Type Conversion
ABAP Core · Chapter-specific technical teaching map
Integration path
Operands
Operator
Conversion
Result
Tutor's chapter 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…
In a real project
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…
Watch out for
• 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…
The situation: a 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.
Explanation
Part 3 introduced the basic data types. Now we combine values: calculations, text processing and conversions.
Arithmetic: + - * / plus DIV (integer division), MOD (remainder) and ** (power) cover the basic operators. lv_total = lv_price * lv_seats. is a typical calculation. The plain / operator gives normal division, DIV gives the integer quotient, and MOD gives the remainder, so 7 DIV 2 equals 3 while 7 MOD 2 equals 1. ADD 1 TO lv_x. and lv_x = lv_x + 1. are fully equivalent ways of writing the same increment.
The result type follows the target field: assigning 7 / 2 to an integer gives 4 (rounded), not 3 — integer targets round commercially rather than truncating, while packed targets simply keep whatever number of decimals they were declared with.
For money specifically, the rule is to use TYPE p (packed) with DECIMALS for amounts — never float, since binary floating point introduces rounding errors that are unacceptable for financial values. Intermediate results in a calculation are computed internally with high precision, and only rounded down to the target field's declared decimals at the very end. Built-in functions like round( val = x dec = 2 ), ceil( x ), floor( x ) and abs( x ) handle the common rounding and sign operations directly.
Text processing relies on a handful of classic statements. CONCATENATE a b INTO c SEPARATED BY space. joins strings together. SPLIT lv_line AT ';' INTO lv_f1 lv_f2 lv_f3. — or INTO TABLE lt_parts — splits a line into pieces. CONDENSE lv_text. removes extra blanks, and adding NO-GAPS removes all blanks entirely. TRANSLATE lv_text TO UPPER CASE. changes case.
REPLACE ALL OCCURRENCES OF ',' IN lv_num WITH '.'. substitutes characters. And SEARCH or FIND 'CARGO' IN lv_text. looks for a substring, coming back with sy-subrc = 0 if it's found. String handling statements: CONCATENATE, SPLIT, CONDENSE, TRANSLATE, REPLACE, SEARCH / FIND, plus strlen( ).
On conversions: MOVE and the assignment operator = convert automatically between compatible types, for example from c to n, or from c to p when the text content happens to be numeric. Conversion exits such as CONVERSION_EXIT_ALPHA_INPUT and CONVERSION_EXIT_ALPHA_OUTPUT handle leading zeros, and WRITE … TO produces formatted output for things like dates and amounts with a CURRENCY addition.
Moving non-numeric text into a number raises CX_SY_CONVERSION_NO_NUMBER — validate input or catch it; in older code, the equivalent protection was checking with CO '0123456789 ' or wrapping the statement in CATCH SYSTEM-EXCEPTIONS.
As a side note on more modern syntax, from release 7.40 onward, CONV type( ) and EXACT type( ) handle conversions more compactly, string functions like to_upper( ) and condense( ) replace the equivalent classic statements, and embedded templates |{ … }| build strings inline — these produce the same results as the classic statements above, just more compactly.
As an exercise, compare 7 / 2 assigned into an integer field versus into a packed field, sum three packed prices and round a share to two decimals, and split a CSV line, clean its fields, and convert an amount with proper error handling. By the end, you should be able to calculate, process text and convert values safely using the classic statements.
Code example
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.Watch out
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.
Do this
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.
In the interview
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
0 of 3 questions attempted
Question 1 of 3
What is the result of lv_i = 7 / 2 when lv_i is of type i?
Question 2 of 3
Which type is correct for a currency amount?
Question 3 of 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?
Practise this in an interview
Answer in your own words and get instant feedback, the way an interviewer would judge it.
1Amount fields show wrong values after arithmetic. What went wrong?
2An inbound interface compares customer number '123' with '0000000123' in the database and finds no match. What is the fix?
Try a related Code Lab
This published exercise covers the same SAP topic. Apply the ideas in a hands-on scenario, then return to review your answers.
Open: Missing SY-SUBRC check after READ TABLE →See it in code
Each example starts from a requirement, shows the design, then the code step by step with a plain-language explanation of every part.
Was this lesson helpful?
Keep exploring
Referenced SAP objects
Reviewed reference pages for the objects this page mentions.
Check before use: lesson examples are educational, not a guarantee of correctness for your SAP edition or release. Sample code has not been executed on your SAP system. Confirm API availability, syntax, authorizations and business impact in an approved development or test environment.