SAP4TECH

SAP and ABAP Free Tutorials
Menu
  • SAP Technical
    • ABAP Code Snippets
    • ABAP WebDynPro
    • SAP GW
    • SAP IDOC (ALE)
    • SAP PI (XI)
    • SAP Screen Personas
    • SAP Workflow
  • SAP Functional
    • SAP FI
    • SAP FSCM
    • SAP HR
    • SAP SD & SAP MM
    • SAP PM
    • SAP PP
    • SAP PS
    • SAP QM
    • SAP VIM
    • SAP WM
  • SAP Fiori
  • SAP HANA
  • SAP BW
  • SAP CRM
  • SAP SRM
  • SAP4TECH
    • About Us
    • Contact Us
    • Terms & Conditions
    • Privacy and Cookie Policy

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

ABAP Tutorials

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

November 9, 2016 John

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

Table of Contents

  • Convert String to Number in ABAP
    • Method 0: Check if a string variable is a number
    • Method 1 : CONVERT_STRING_TO_INTEGER
    • Method 2: CATS_ITS_MAKE_STRING_NUMERICAL
    • Method 3: WOSI_CONVERT_STRING_TO_NUM
    • Method 4: Convert String to Integer the ABAP OO way
    • Method 5: Do it directly in ABAP
  • SAP ABAP Convert string to decimal in ABAP
    • Method 1: With ABAP coding (the “+” statement)
    • Method 2: WSAF_CONV_STRING_TO_DECIMAL
    • Method 3: MOVE_CHAR_TO_NUM
  • Convert String to Quantity in ABAP
    • Try 1: CHECK_AND_CONVERT_NUMERICS
    • Try 2: CL_FDT_QUANTITY_CONV Class
    • Try 3: Convert String to Quantity with RS_CONV_EX_2_IN
    • Try 4: ABAP Write Statement (Specific Output)
    • Try 5: /CWM/CONVERT_CHAR_TO_PACK
  • ABAP Convert String to Amount (Currency)
    • Method 1: HRCM_STRING_TO_AMOUNT_CONVERT
    • Method 2: PSSV_TEXT_INTO_FIELD_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 :

ABAP
1
2
3
4
5
6
7
8
9
10
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:

ABAP
1
2
3
4
5
6
7
8
9
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):

ABAP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.

ABAP
1
2
3
4
5
6
7
8
9
10
11
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)

ABAP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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:

ABAP
1
2
3
4
5
6
7
DATA: LV_STRING  TYPE STRING.
DATA: LV_INTEGER TYPE I.
 
LV_STRING  = '01234567890'.
LV_INTEGER = LV_STRING.
 
WRITE LV_INTEGER.

You may check also

  • Conversion ABAP: between Binary, String, XString and Table
  • Master the ABAP Date Calculation with ABAP Code example

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:

ABAP
1
2
3
4
5
6
7
8
9
10
11
12
13
    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:

ABAP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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

ABAP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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)

ABAP
1
2
3
4
5
6
7
8
9
10
11
12
13
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):

ABAP
1
2
3
4
5
6
7
8
9
10
11
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

1
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

ABAP
1
2
3
4
5
6
7
8
9
10
11
    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.

ABAP
1
2
3
4
5
6
7
8
9
10
11
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           "
    .

Prev Article
Next Article
Tags:Convert Decimal String

Related Articles

SAP File
How to zip a file using ABAP: The easy why …

How to zip a file using ABAP in SAP with CL_ABAP_ZIP

SAP ABAP
SAP Change Pointer?allows to trigger change on SAP Master Data …

SAP Change Pointer Overview with Tcodes, Tables, Function and BADi

Search on SAP4TECH

The Most Populars

  • The Most Important SAP ISU Tables
    The Most Important SAP ISU Tables
  • SAP Users Tables
    SAP Users Tables (for Personal, Logon, and Address Data)
  • List of SAP Purchase Order Tables in SAP MM SAP PO Tables
    SAP Purchase Order Tables: Main PO tables in SAP MM – SAP PO Tables
  • Full list of SAP Movement Types
    SAP Good Movement Types – Full list of SAP Movement Types
  • The Main SAP GL Account Tcodes SAP GL Account Tables
    The Main SAP G/L Account Tcodes & SAP GL Account Tables
  • SAP BOM Tables
    SAP BOM Tables for BOM Header, Items and components and Category

Related Posts

  • SAP IDOC Qualifier
    All what you need to know about SAP IDOC Qualifier Overview
  • Regular Expressions in SAP ABAP
    Using Regular Expressions in SAP ABAP (REPLACE, FIND REGEX)
  • tax 468440 1280 e1478961032985
    Step by Step tutorials How to translate SAP Smartforms ?
  • SAP CRM Catalog e1461269538601
    How to Update SAP Product Hierarchy Category on SAP CRM with full ABAP Code
  • SAP Process Integration
    Important SAP PI Tcodes (Process Integration Transaction Codes) – SAP XI Tcodes

SAP4TECH

SAP and ABAP Free Tutorials

Trending Posts

  • The Most Important SAP ISU Tables
  • SAP Users Tables (for Personal, Logon, and Address Data)
  • SAP Purchase Order Tables: Main PO tables in SAP MM – SAP PO Tables
  • SAP Good Movement Types – Full list of SAP Movement Types
  • The Main SAP G/L Account Tcodes & SAP GL Account Tables
  • SAP BOM Tables for BOM Header, Items and components and Category

The Most Recents

  • The Most Important SAP ISU Tables
  • SAP Fiori 3 UX and Design of SAP Fiori Apps for SAP S/4HANA, SAP TechEd Lecture
  • The Main SAP Dunning Transaction Codes
  • SAP Accounts Payable Tcodes & Accounts Receivable Tcodes ( SAP AP Tcodes & SAP AR Tcodes)
  • The Most Important SAP Payment Terms Tables (ZTERM, Text…)

Search

SAP Tutorials by Topics

  • SAP Tables
  • SAP Tcodes
  • SAP BAPI
  • ABAP Snippets
  • Top SAP Courses
  • Top SAP Books
Copyright © 2021 SAP4TECH

Ad Blocker Detected

Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Refresh
This website uses cookies to improve your experience. We'll assume you accept this policy as long as you are using this websiteAcceptView Policy