Dynamic Handling of Decimal Places for Amounts
Currency in SAP is largely handled extensively with the currency functionality and TCURC and TCURX tables. However, every now and then we come across a requirement where we need to extend this to accommodate other requirements.
In this blog we would like to share our experience of a requirement we came across in our project where the number of decimal places for a currency needed to be increased in order to attain more precision.
The requirement for this extension is usually when the user needs more precision in calculations based on an amount. This can be a rate charged per unit quantity, this rate (with higher decimal places) when multiplied with a large total quantity ends up giving a huge difference in net amount (as compared to normal currency of USD/ JPY etc.)
Thus, to cater to this requirement we provided the user with an option to set the number of decimal places dynamically at run time without changing the currency from the standard ISO currencies.
Suppose that the customer requirement is to use up to 5 decimal places for a ‘RATE’ field for all currencies irrespective of the total number of decimal places allowed for the currency as per the TCURC and TCURX tables.
The user can choose the number of decimal places for a currency based on the scenario and all the subsequent calculations for that scenario will take place based on the chosen number of decimal places.
The challenge here is that ‘RATE’ field is always associated to a reference ‘CURRENCY’ field in the structure/table. This reference puts a stringent check on the number of decimal places allowed for a specific currency. This number is derived from the TCURX and TCURC table and the customer is bound to use the same decimal places for the currency for all the scenarios.
Although there is a standard approach for maintaining different decimal places for the same currency using the TCURC and TCURX tables, but with the standard approach the user has to do so for all the currencies in use. In case we need to use 20 Currencies with up to 5 decimal place, the user will have to maintain 100 entries in both the tables.
Also, with the standard approach we would see a different currency such as USDN with the different decimal places. Although this USDN would in turn be linked to the ISO currency( USD in this case), the user will see USDN instead of USD on the UI which is not always desirable.
Our solution approach (described below) intends to reduce the manual effort in maintaining several records for different currencies in TCURC and TCURCX table. Also, the end user always sees a consistent currency on the UI irrespective of the number of decimal places. (no nuances such as ‘USDN’ etc).
Suppose that the database table/structure contains two fields ‘RATE’ and ‘CURRENCY’ (as a reference currency field for the ‘RATE’ field)
The following changes must be made to the database table:
- Create a new field ‘CURRENCY_EXT’ with the data type as ‘WAERS’ in the same table/structure.
2. Add the CURRRENCY field as a REFERENCE FIELD for the ‘RATE’ field.
3. Do the same process of referencing the CURRENCY field and adding CURRENCY_EXT field in all relevant structures.
4. We would now populate the CURRENCY field with dummy currencies which have a decimal value as per our requirement.
- These dummy currencies can be fetched from a customizing based on the number of decimal places, or in case the requirement is for a fixed decimal place it can be filled directly.
- These Dummy currencies must be maintained by the customer in TCURX and TCURC tables. The procedure for the same is explained below.
- The method GET_CURRENCY_DECIMALS is used to fetch the currency by reading from the customizing that we have maintained and then fetching the details from the TCURX and TCURC tables.
- Note: We check the TCURX table to just to ensure that the currency maintained in the customizing is also maintained in the standard currency tables.
- At all places on the UI we will display the CURRENCY_EXT field to the end user. Whereas the CURRENCY field would be hidden from display. This in turn would display the rate in the required decimal format as the CURRENCY field stores the internal currency and is referenced to the rate.
The need for varying decimal places for a currency in ABAP can be handled with help of standard TCURC and TCURX tables.
However, the approach explained above is useful when there is a need for a consistent user experience with regards to the currency with varying decimal places. Also, this approach reduces the manual effort for maintenance of records in TCURC and TCURX tables for separate currencies for different decimal places.
A few points to note:
- We must reference the CURRENCY_INT field to the rate in all structures and not just in UI structures. This is to ensure that the calculations are done with the right decimal place.
- For currency conversion calculations we would still use CURRENCY field as it contains the ISO currencies as maintained by the end user.