ABAP: How to Convert String to Integer, Quantity, Currency

Convert String to Number and String in ABAP : Find here the different methods to convert String variable into Number, Quantity or Amount in SAP ABAP with sample code or SAP standard function template making easing to convert string.

You will find mainly how to Convert String to Number in ABAP, SAP ABAP Convert string to decimal in ABAP, Convert String to Quantity in ABAP and ABAP Convert String to Amount (Currency).

Convert String to Number in ABAP

In this part, I will try to explain different methods how to sap ABAP convert char to number:

Method 0: Check if a string variable is a number

First at all, let’s check if a string variable (or a char variable) contains really only number before trying to convert it.

Fortunately, SAP provides in Standard a useful function NUMERIC_CHECK

Here the signature of NUMBERIC_CHECK :

The HTYPE export variable can have:

  • CHAR: if the variable doesn’t contain only numerical characters.
  • NUMC: if the variable contains exclusively Number (integer).

Tips: CHECK lv_variable NA SY-ABCD : check if an ABAP variable doesn’t contain letters and none-numerical characters 😉

Method 1 : CONVERT_STRING_TO_INTEGER

IF you are looking to convert string to integer in ABAP code, you should start by testing this function CONVERT_STRING_TO_INTEGER.

Here the signature of this function:

Unfortunately, it is not available on each system. It is shipped with ECC.

Method 2: CATS_ITS_MAKE_STRING_NUMERICAL

In order to convert in SAP ABAP String variable to Number or Integer, you can also use the standard Function CATS_ITS_MAKE_STRING_NUMERICAL.

Here a sample ABAP Code how to use it (source):

Method 3: WOSI_CONVERT_STRING_TO_NUM

An other method to convert string variable to numerical format is the function WOSI_CONVERT_STRING_TO_NUM.

Here the signature of this signature.

Method 4: Convert String to Integer the ABAP OO way

If you prefer using the ABAP OO way, you can use the method read_container_co of the ABAP utility class (CL_ABAP_CONTAINER_UTILITIES).

Personally, I have never used before this method (but I used a lot the ABAP utilities classes to read the metadata of ABAP variables and do some generic programming in ABAP).

Here a sample ABAP code using this method (>source)

Method 5: Do it directly in ABAP

Instead of using function modules or method’s classes, you can do convert string / char to Integer or numerical using sample ABAP Statement.

The MOVE Statement and WRITE Statement can do the job for you or even the affectation statement (=) will do the casting.

A very example ABAP Code snippet can be:

You may check also

SAP ABAP Convert string to decimal in ABAP

Method 1: With ABAP coding (the “+” statement)

The easiest way to convert a char variable to decimal is to use the + statement in order to sum a decimal variable and a string.

The ABAP engine will cast the string variable into a decimal and the result will be a decimal.

Here a sample ABAP program illustrating this method:

Method 2: WSAF_CONV_STRING_TO_DECIMAL

Once again, SAP provides this function WSAF_CONV_STRING_TO_DECIMAL to convert a string to decimal.

The signature of this function module is:

Method 3: MOVE_CHAR_TO_NUM

MOVE_CHAR_TO_NUM can do the job and make the conversion from Char to Decimal.

Look at the following sample (source)

Convert String to Quantity in ABAP

Let’s jump to the Quantity part. I want to make a confidence: the MENGE data makes my life much harder because of all the dump of illegal data type occurred to Quantity data type.

So let’s remove this pain and list some method how to convert String to Quantity or Amount variable.

PS: I will be called it try and not method because the functions are not available on every version of SAP.

Try 1: CHECK_AND_CONVERT_NUMERICS

The first method can be the function module CHECK_AND_CONVERT_NUMERICS to Checks whether a value is numeric and converts to a packed number

Try 2: CL_FDT_QUANTITY_CONV Class

Here we are talking about ABAP OO again. Actually it is the standard nowadays.

The Class CONVERT_STRING_TO_QUANTITY have an interesting method to convert String to Quantity (source)

Try 3: Convert String to Quantity with RS_CONV_EX_2_IN

The third try to convert String to Quantity is with function RS_CONV_EX_2_IN.

Here a sample ABAP code to figure out the use of the function RS_CONV_EX_2_IN (source):

Try 4: ABAP Write Statement (Specific Output)

Check more about the Special Output Formats on help.sap.com and make a focus on the Unit-specific Output Formats’s section.

The statement to be use is: WRITE <f> UNIT <u>.

Just try this ABAP snippet code

Try 5: /CWM/CONVERT_CHAR_TO_PACK

This function /CWM/CONVERT_CHAR_TO_PACK will convert a char to pack ABAP type.

ABAP Convert String to Amount (Currency)

Method 1: HRCM_STRING_TO_AMOUNT_CONVERT

Here a same use of HRCM_STRING_TO_AMOUNT_CONVERT in order to convert in ABAP String to Amount

Method 2: PSSV_TEXT_INTO_FIELD_CURRENCY

The next method is PSSV_TEXT_INTO_FIELD_CURRENCY can be also be used to convert String to Amount in ABAP.

Here the signature of PSSV_TEXT_INTO_FIELD_CURRENCY.