Skip to Content
Technical Articles
Author's profile photo Alwin van de Put

Formatting amounts with Currency symbol – An ABAP Class solution

Formatting amounts is not only dependent on the currency, but is also dependent on the country. Here some examples:

Country Currency Example amount Symbol position (1) Space in between (2) Decimal sign (3) Number of decimals (4) Currency symbol (5)
USA Dollar $1,234.57 Left No Point 2 $
Germany Euro 1.234,57 € Right Yes Comma   2
Netherlands Euro € 1.234,57 Left Yes Comma 2
Belgium Euro €1 234,57 Left No Comma 2

There are 3 characteristics of an amount format which are country specific.

1. Symbol position. (In USA it is left, in Germany right.)
2. Space in between symbol and amount. (In USA no space, Netherlands one space.)
3. Decimal sign.

And 2 characteristics are currency specific

4. Number of decimals.
5. Currency symbol.

Characteristic 3 and 4 can be retrieved from SAP database tables.
3. Decimals sign: SAP field “Decimal Format” (T005X-XDEZP).
4. Number of decimals: SAP field “Number of decimal places”  (TCURX-CURRDEC).

So for characteristics 1, 2 and 5 we need an extra table.

Currency ABAP class

I created a class to format amounts based on the country and currency. The class name is ZCUR_CURRENCY_BO.

Get formatted amount in ABAP.

    "Get currency object
    DATA(lo_currency_bo) =
      zcur_currency_bo_ft=>get_factory( )->get_currency_bo(
        iv_country_key = <ls_currency>-country_key
        iv_currency_key = <ls_currency>-currency_key ).

    "Format amount
    DATA(lv_formatted_amount) = lo_currency_bo->format_amount( lv_amount ).

For example:

  • iv_country_key = ‘US’
  • iv_currency_key = ‘USD’
  • lv_formatted_amount = $1,234.57

How does it work?

In class ZCUR_CURRENCY_BO_DP in the CLASS_CONSTRUCTOR method the table GT_CURRENCY_LIST is filled with the characteristics 1, 2 and 5.

For example:

country_key ‘US’ currency_key ‘USD’ left_symbol |$| right_symbol || )
country_key ‘DE’ currency_key ‘EUR’ left_symbol || right_symbol | €| )

Characteristics:

  • 1. Symbol position
    The symbol is placed in the field left_symbol or right_symbol.
  • 2. Space in between symbol and amount
    The space is placed with the left_symbol or right_symbol. See for example the German right_symbol = | €|.
  • 5. Currency symbol
    The currency symbol is placed in the field left_symbol or right_symbol.

Remarks

  • Table GT_CURRENCY_LIST is not yet complete.
    You can extend it in your own system.
    Program ZCUR_CURRENCY_BO_LIST can be used to create a list of all currencies.
    The colum “L/R” = “Left or right symbol indicator” indicates whether a currency has a left or right symbol.
    If you have some updates for me with these currency symbols, than please send them to me via blogs.sap.com or via Github.
  • If a currency does not have a left_symbol or right_symbol, than the Currency ISO code will be placed to right of the amount with a space in between. For example for Egypt 1,234.57 EGP.
  • Always check with program ZCUR_CURRENCY_BO_LIST the currency you use.
    Don’t assume all symbols are correct. And test it wherever you use it.
    It is about money, so you should always be careful.

Using the framework

  1. Be sure you have program ZABAPGIT in your SAP system.
    See https://docs.abapgit.org/.
  2. Import the Github repository:
    https://github.com/alwinvandeput/abap_amount_formatting_with_currency_symbol
    … in your SAP system.
  3. Start program ZCUR_CURRENCY_BO_LIST to see the results.

Kind regards,

Alwin van de Put

Assigned tags

      7 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Shai Sinai
      Shai Sinai

      An interesting project. Thanks for sharing.

      I think you may use the common CLDR for maintenance of currencies (GT_CURRENCY_LIST).

      Author's profile photo Alwin van de Put
      Alwin van de Put
      Blog Post Author

      Thank you for your support and the link. It should contain what I'm looking for.

      Author's profile photo Matthew Billingham
      Matthew Billingham

      I like it. May I suggest two improvements?

      1. Define the non-factory classes with respect to interfaces
      2. Add unit tests.

       

      Author's profile photo Alwin van de Put
      Alwin van de Put
      Blog Post Author

      Thank you for your improvements. I will make both changes to the code.

      Author's profile photo Enno Wulff
      Enno Wulff

      I never understood why there are no CURRENCY and no UNIT functions provided with the string templates.

      like

      data(info) = |{ struc-qty UNIT = struc-meins } { struc-meins } cost {
                      struc-price CURRENCY struc-waers } { mystruc-waers }|.
      Author's profile photo Johann Fößleitner
      Johann Fößleitner

      Hi Enno,

      you are right, UNIT is not available - but CURRENCY is available with string templates.

      template = |{ price currency = 'JPY' number = USER }|.

      Regards, Föß

       

      Author's profile photo Enno Wulff
      Enno Wulff

      thanks Johann Fößleitner ! I was sure that didn't exist when I was reassuring my statement... 😀