Skip to Content

In this article I’d like to show an easy approach on how to convert an amount given in one currency to another currency and warn you before a possible bug in SAP standard FM used for currency conversions.


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 of the program is:

CURRENCY_CONVERSION01.png

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 VERY 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 of the second example:

CURRENCY_CONVERSION02.png

Original of the article is at my blog (oprsteny.com)

To report this post you need to login first.

1 Comment

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

  1. Shai Sinai

    The reason behind this surprising result is the mysterious attribute “Fixed point arithmetic”.

    If you’ll check the properties of function group SCUN, you may see that “Fixed point arithmetic” isn’t checked (like in several ancient source codes). In such case, packed numbers behaves “unexpectedly”.

    Hope it unravel the mystery…

    (0) 

Leave a Reply