Converting String to Decimal in SAP ABAP
In SAP ABAP, converting a string (STRING or CHAR) to a decimal (DEC, FLTP, CURR, or QUAN) requires careful handling, especially if the string contains thousands separators, decimal points, or currency symbols.
1. Simple Conversion (Direct Assignment)
If the string contains a valid numeric format, ABAP allows direct assignment:
DATA: lv_string TYPE string VALUE '123.45', lv_decimal TYPE p DECIMALS 2.
lv_decimal = lv_string.
WRITE: lv_decimal. " Output: 123.45✅ Works if the string is in a standard decimal format (123.45).
❌ Fails if the string contains extra symbols ($123.45 or 1,234.56).
2. Using CONVERT INTO (Handling Different Formats)
If the string contains commas or decimal separators, use CONVERT INTO.
DATA: lv_string TYPE string VALUE '1,234.56', lv_decimal TYPE p DECIMALS 2.
CONVERT lv_string INTO lv_decimal.
WRITE: lv_decimal. " Output: 1234.56- This method automatically ignores thousands separators (
1,234.56 → 1234.56). - If the string is invalid,
sy-subrcwill be set.
3. Using FLTP (Floating Point Conversion)
For floating-point values, directly assign to FLTP type:
DATA: lv_string TYPE string VALUE '123.456', lv_float TYPE fltp.
lv_float = lv_string.
WRITE: lv_float. " Output: 123.456FLTPsupports more precision thanDEC.
4. Handling Different Decimal Separators (CONVERT_TEXT_TO_NUMBER)
If the decimal separator is locale-dependent (e.g., 1.234,56 in German format), use CONVERT_TEXT_TO_NUMBER.
DATA: lv_string TYPE string VALUE '1.234,56', lv_decimal TYPE p DECIMALS 2.
CALL FUNCTION 'CONVERT_TEXT_TO_NUMBER' EXPORTING i_text = lv_string IMPORTING e_number = lv_decimal.
WRITE: lv_decimal. " Output: 1234.56- Handles locale-specific formats (
1.234,56 → 1234.56). - Works for both commas and dots.
5. Removing Non-Numeric Characters (Regex Cleaning)
If the string contains currency symbols or extra text, remove them first:
DATA: lv_string TYPE string VALUE '$1,234.56', lv_cleaned TYPE string, lv_decimal TYPE p DECIMALS 2.
REPLACE ALL OCCURRENCES OF REGEX '[^0-9.,]' IN lv_string WITH ''.lv_cleaned = lv_string.
CONVERT lv_cleaned INTO lv_decimal.
WRITE: lv_decimal. " Output: 1234.56- Removes all non-numeric characters (
$, spaces, text). - Then, converts cleaned string to decimal.
6. Handling Invalid Input (Validation)
Always check if the string is numeric before converting.
DATA: lv_string TYPE string VALUE 'ABC123.45', lv_decimal TYPE p DECIMALS 2.
TRY. lv_decimal = lv_string. WRITE: 'Valid Number:', lv_decimal. CATCH cx_sy_conversion_error. WRITE: 'Invalid Number!'.ENDTRY.- If the string contains letters (
ABC123.45), it throws a conversion error.
🚀 Summary: Best Methods for String-to-Decimal Conversion
| Method | Use Case |
|---|---|
lv_decimal = lv_string | Simple, standard numeric format (123.45) |
CONVERT INTO | Handles thousands separators (1,234.56) |
FLTP assignment | Floating point conversion (123.456) |
CONVERT_TEXT_TO_NUMBER | Locale-dependent (1.234,56 → 1234.56) |
REGEX (cleaning) + CONVERT | Removes $, €, or text before conversion |
TRY...CATCH cx_sy_conversion_error | Validates if the string is numeric |