ABAP – Currency conversions

SAPIn this article I’d like to show an easy approach on how to convert an amount given in one currency to another currency.

DATA:
  l_in(15) TYPE p DECIMALS 5,
  l_out(15) TYPE p DECIMALS 5.

l_in = 1.

CALL FUNCTION 'CONVERT_TO_LOCAL_CURRENCY'
  EXPORTING
    date             = sy-datum
    foreign_amount   = l_in
    foreign_currency = 'EUR'
    local_currency   = 'DKK'
  IMPORTING
    local_amount     = l_out
  EXCEPTIONS
    no_rate_found    = 1
    overflow         = 2
    no_factors_found = 3
    no_spread_found  = 4
    derived_2_times  = 5
    OTHERS           = 6.
IF sy-subrc = 0.
  WRITE: l_in, 'EUR = ', l_out, 'DKK'.
ENDIF.

Result:
01 Currency conversion

IMPORTANT NOTE (valid at 2014-05-27): It is VERY important to use variables defined equally – having the same precision/decimal places and length. Otherwise the results might be quite surprising: check the following modification with different decimal precision on INPUT and OUTPUT

DATA:
  l_in(15) TYPE p DECIMALS 5,
  l_out(15) TYPE p DECIMALS 3.

l_in = 1.

CALL FUNCTION 'CONVERT_TO_LOCAL_CURRENCY'
  EXPORTING
    date             = sy-datum
    foreign_amount   = l_in
    foreign_currency = 'EUR'
    local_currency   = 'DKK'
  IMPORTING
    local_amount     = l_out
  EXCEPTIONS
    no_rate_found    = 1
    overflow         = 2
    no_factors_found = 3
    no_spread_found  = 4
    derived_2_times  = 5
    OTHERS           = 6.
IF sy-subrc = 0.
  WRITE: l_in, 'EUR = ', l_out, 'DKK'.
ENDIF.

Result:
02 Currency conversion

Leave a Reply