Convert String to Number and Quantity in ABAP

Different Methods to Convert String to Integer, Quantity, Currency in SAP ABAP

Convert String to Number and String in ABAP : Discover 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. 

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 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.
Fortunalty, SAP provides in Standard an usefull 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).

TipsCHECK lv_variable NA SY-ABCD : check if an ABAP variable doesn’t contains letters and none-numerical caracters 😉 

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:

Unfortunaly, 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 ):

Check also   Add Leading Zero ABAP

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 thi method ( but I used a lot the ABAP utilities classes to read the meta data 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 variabl 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 fct module is:

Check also   SAP Check Employee Existence and Status

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 dumb of illegal data type occured du to Quantity data type. So let’s remove this pain and list some of 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 n°1: CHECK_AND_CONVERT_NUMERICS

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

Try n°2: CL_FDT_QUANTITY_CONV Class

Here we are talking about ABAP OO again. Acutally it is the standard nowdays.
The Class CONVERT_STRING_TO_QUANTITY have an interesting method to convert String to Quantity (source)

Check also   All what you need to know about SAP IDOC Qualifier Overview

Try n°3: Convert String to Quantity with 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 n°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 n°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

Method n°2: PSSV_TEXT_INTO_FIELD_CURRENCY

 

Leave a Reply

Your email address will not be published. Required fields are marked *