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

Convert String to Number and Quantity in ABAP

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 :

FUNCTION numeric_check.
*"----------------------------------------------------------------------
*"Lokale Schnittstelle:
*"       IMPORTING
*"             STRING_IN
*"       EXPORTING
*"             STRING_OUT
*"             HTYPE LIKE DD01V-DATATYPE
*"----------------------------------------------------------------------
ENDFUNCTION.

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:

CALL FUNCTION 'CONVERT_STRING_TO_INTEGER'
  EXPORTING
    p_string =                  "
  IMPORTING
    p_int =                     " i
  EXCEPTIONS
    OVERFLOW = 1                "
    INVALID_CHARS = 2           "
    . 

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

DATA : LV_STR TYPE STRING,
       LV_NUM LIKE CATS_ITS_FIELDS-NUM_VALUE.
 
LV_STR = '123456789'.
 
CALL FUNCTION 'CATS_ITS_MAKE_STRING_NUMERICAL'
  EXPORTING
    INPUT_STRING  = LV_STR
  IMPORTING
    VALUE         = LV_NUM
  EXCEPTIONS
    NOT_NUMERICAL = 1
    OTHERS        = 2.
 
WRITE : LV_NUM.

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.

CALL FUNCTION 'WOSI_CONVERT_STRING_TO_NUM' "
  EXPORTING
    p_action =                  " wosit_functioncode
    p_string_100 =              " char100
  IMPORTING
    p_erfmg =                   " wosi_iseg-erfmg
    p_exvkw_posws =             " wosi_iseg-exvkw_posws
    p_swert =                   " wosi_head-swert
    p_vkumg =                   " wosi_umb_item-vkumg
  EXCEPTIONS
    ERROR_IN_SY_MSG = 1         " .

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)

read_container_c" >data : int type i.
data : char(10) type c value '123'.
 
class cl_abap_container_utilities definition load.
 
call method cl_abap_container_utilities=>read_container_c
  exporting
    im_container           = char
  importing
    ex_value               = int
  exceptions
    illegal_parameter_type = 1
    others                 = 2.
 
write : int.

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:

DATA: LV_STRING  TYPE STRING.
DATA: LV_INTEGER TYPE I.

LV_STRING  = '01234567890'.
LV_INTEGER = LV_STRING.

WRITE LV_INTEGER.

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:

    DATA: lv_string   TYPE string.
    DATA: lv_decimal  TYPE p DECIMALS 2.


    lv_string = '574,980.17'.

    TRANSLATE lv_string USING ', '.

    CONDENSE lv_string NO-GAPS.
    CLEAR lv_decimal.
    lv_decimal = lv_decimal + lv_string.

    WRITE:/ lv_decimal.

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:

CALL FUNCTION 'WSAF_CONV_STRING_TO_DECIMAL'
  EXPORTING
    number_to_convert =         " dynfnam
    decimal_property =          " wsaf_s_table_defines
  IMPORTING
    decimal_15_3 =              " wsaf_bracketlow
    decimal_13_3 =              " wsaf_currsales
    decimal_11_2 =              " wsaf_purchunit
    decimal_5_0 =               " wsaf_scalefactor
    decimal_4_0 =               " wsaf_expiration
    decimal_3_0 =               " wsaf_availdays
  EXCEPTIONS
    INTERNAL_ERROR = 1          "
    WRONG_CHARACTERS = 2        "
    FIRST_CHARACTER_WRONG = 3   "
    ARITHMETIC_SIGN = 4         "
    MULTIPLE_DECIMAL_SEPARATOR = 5  "
    NUMBER_TOO_BIG = 6          "
    .

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)

DATA: lv_chr(4) type c,
      lv_num    type p.
 
CALL FUNCTION 'MOVE_CHAR_TO_NUM'
  EXPORTING
    CHR             = lv_chr
  IMPORTING
    NUM             = lv_num
  EXCEPTIONS
    CONVT_NO_NUMBER = 1
    CONVT_OVERFLOW  = 2
    OTHERS          = 3.
IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

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

CALL FUNCTION 'CHECK_AND_CONVERT_NUMERICS' 
  EXPORTING
*   dfeld = SPACE               "               Name of DDIC field
    dmzei =                     "               Decimal points '.' or ','
    dtype =                     "               Data type of screen field
*   dypno = SPACE               " d020s-dnum    Screen number
    efeld =                     "               Field with value in character format
*   fname = SPACE               "               Screen field name
*   progr = SPACE               " d020s-prog    Program name
*   imp_decimals = '0'          "               Explicit entry of decimal places (without DFELD)
  IMPORTING
    error =                     "               Indicator for error message
    ifeld =                     "               Wert in packed format
    messg =                     " message       Message
    msgln =                     "               Length of message
    .  "  CHECK_AND_CONVERT_NUMERICS

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)

CONVERT_STRING_TO_QUANTITY" >DATA: 
   ld_IV_TEXT TYPE STRING , 
   ld_RS_QUANTITY TYPE REF TO IF_FDT_TYPES=>ELEMENT_QUANTITY. 

" ld_IV_TEXT = "<Populate with value> 

CALL METHOD CL_FDT_QUANTITY_CONV=>CONVERT_STRING_TO_QUANTITY(
EXPORTING
   IV_TEXT = ld_IV_TEXT
RECEIVING
   RS_QUANTITY = ld_RS_QUANTITY
EXCEPTIONS
   CX_FDT_CONVERSION = 4 ).

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

DATA ls_tabfield TYPE tabfield.
 
ls_tabfield-tabname    = 'MARA'.
ls_tabfield-fieldname  = 'NTGEW'.
 
CALL FUNCTION 'RS_CONV_EX_2_IN'
  EXPORTING
    input_external                     = '7.123,897'
    table_field                        = ls_tabfield
  IMPORTING
    output_internal                    = mara-ntgew.

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

WRITE LV_STRING UNIT LV_UNIT to LV_QUANTITY.

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

    DATA: LV_num TYPE p DECIMALS 2.

    CALL FUNCTION 'HRCM_STRING_TO_AMOUNT_CONVERT'
      EXPORTING
        string              = '1234567.19'
        decimal_separator   = '.'
        thousands_separator = ','
      IMPORTING
        betrg               = lv_num.

    WRITE lv_num LEFT-JUSTIFIED.

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.

CALL FUNCTION 'PSSV_TEXT_INTO_FIELD_CURRENCY' "
  EXPORTING
    i_amount =                  "
    i_currency =                "
  IMPORTING
    e_amount =                  "
  EXCEPTIONS
    WRONG_AMOUNT = 1            "
    WRONG_CURRENCY = 2          "
    WRONG_DECIMAL = 3           "
    .