Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member
0 Kudos

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 bapicurr-bapicurr,
        lv_internal TYPE bseg-wrbtr,
        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 sy-uname.

***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 NO-GAPS.
  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.