Skip to Content

Hi All,

If we need to convert amount format based on country then we can use the below code snippet to change the format to the particular format followed by any country. For this we require the currency key and the amount that needs to be converted. The output that we get is in the required format.

FUNCTION z_currency_conversion .
*”———————————————————————-
*”*”Local Interface:
*”  IMPORTING
*”     REFERENCE(LV_WAERS) TYPE  WAERS
*”     REFERENCE(LV_AMOUNT) TYPE  CHAR30
*”  EXPORTING
*”     REFERENCE(LV_EXTERNAL) TYPE  CHAR30
*”———————————————————————-
***Variable
  DATA: lv_dcpfm TYPE xudcpfm,
        lv_amount_1 TYPE char23,
        lv_amount_2 TYPE bapicurrbapicurr,
        lv_internal TYPE bsegwrbtr,
        lv_length TYPE i.

***Constants
  CONSTANTS: lc_x TYPE xudcpfm VALUE ‘X’,
             lc_y TYPE xudcpfm VALUE ‘Y’,
             lc_dot TYPE c VALUE ‘.’,
             lc_com TYPE c VALUE ‘,’,
             lc_con TYPE char2 VALUE ‘,.’.

  lv_amount_1 = lv_amount.
***Fetch records from USR01
  CLEAR: lv_dcpfm.
  SELECT SINGLE dcpfm FROM usr01 INTO lv_dcpfm WHERE bname EQ syuname.

***Change the format
  IF lv_dcpfm EQ lc_x.
    REPLACE ALL OCCURRENCES OF lc_com IN lv_amount_1 WITH space.
  ELSEIF lv_dcpfm EQ space.
    REPLACE ALL OCCURRENCES OF lc_dot IN lv_amount_1 WITH space.
    TRANSLATE lv_amount_1 USING lc_con.
  ELSEIF lv_dcpfm EQ lc_y.
    TRANSLATE lv_amount_1 USING lc_con.
    CONDENSE lv_amount_1 NOGAPS.
  ENDIF.

  CLEAR: lv_length.
  DESCRIBE FIELD lv_amount_1 LENGTH lv_length IN CHARACTER MODE.
  lv_amount_2 = lv_amount_1.

***Convert to internal SAP format
  CALL FUNCTION ‘BAPI_CURRENCY_CONV_TO_INTERNAL’
    EXPORTING
      currency             = lv_waers
      amount_external      = lv_amount_2
      max_number_of_digits = lv_length
    IMPORTING
      amount_internal      = lv_internal.
  IF lv_internal IS NOT INITIAL.
***Convert to SAP external format
    WRITE lv_internal TO lv_external CURRENCY lv_waers.
  ENDIF.
ENDFUNCTION.

Hope it serves useful to you.

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply