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

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.456

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

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

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.

🚀 Summary: Best Methods for String-to-Decimal Conversion

MethodUse Case
lv_decimal = lv_stringSimple, standard numeric format (123.45)
CONVERT INTOHandles thousands separators (1,234.56)
FLTP assignmentFloating point conversion (123.456)
CONVERT_TEXT_TO_NUMBERLocale-dependent (1.234,56 → 1234.56)
REGEX (cleaning) + CONVERTRemoves $, , or text before conversion
TRY...CATCH cx_sy_conversion_errorValidates if the string is numeric