Decimal Shift & Decimal Shift Back in CURRENCY_CONVERSION an Example
Hello All,
May be you all are aware with this, but i thought i should share this simple example.
Below example explains the use of parameter DECIMAL_SHIFT and DECIMAL_SHIFT_BACK in CDS function CURRENCY_CONVERSION (ABAP 7.40).
First let us understand what does Standard definition says:
DECIMAL_SHIFT– If the value “X” is passed to the parameter decimal_shift, the value passed is multiplied by 10 to the power of two minus the number of decimal places of the source currency before it is converted.
DECIMAL_SHIFT_BACK– If the value “X” is passed to the parameter decimal_shift_back, the result is divided by 10 to the power of two minus the number of decimal places of the target currency before it is converted.
But how its linked to each other (currency exchange rates/ Decimal)
Exchange rates are stored in table TCURR , Decimal in currency gets stored in TCURX (Below values in the example tables can be different , its created for example purpose)
Below CDS view has been defined to apply conversion over DEMO_PRICES table
Demo_Prices table data:
in this example target currency is USD only, even though we can take input via parameter also.
Case 1 Without Rounding , DECIMAL_SHIFT is On, DECIMAL_SHIFT_BACK is OFF
lets check the output
means in rounding pass ‘ ‘, DECIMAL_SHIFT will be ‘X’ and DECIMAL_SHIFT_BACK will be ‘ ‘
Output :
Amount = 1 . From EUR to USD 
Exchange rate as per table EUR = USD * 2 Decimal shift is on so value passed*10 power(2 – number of decimal in source currency) Value passed = 1*10^(20) = 100 Now apply Exchange = 100 * 2 = 200

Amount = 1 . From GBP to USD 
Exchange rate as per table GBP = USD * 1.6 Decimal shift is on so value passed*10 power(2 – num of decimal in source currency) Value passed = 1*10^(20) = 100 Now apply Exchange = 100 * 1.6 = 160

Amount = 1 . From JPY to USD 
Exchange rate as per table JPY = USD / 114.75 Decimal shift is on so value passed*10 power(2 – num of decimal in source currency) Value passed = 1*10^(20) = 100 Now apply Exchange = 100 / 114.75 = 0.87 
Amount = 1 . From USD to USD 
Exchange rate as per table USD = USD * 1 Decimal shift is on so value passed*10 power(2 – number of decimal in source currency) Value passed = 1*10^(20) = 100 Now apply Exchange = 100 * 1 = 100 
Amount = 12345 . From USDN to USD 
Exchange rate as per table USDN = USD / 1 Decimal shift is on so value passed*10 power(2 – number of decimal in source currency) Value passed = 12345*10^(25) = 12345*10^3 = 12.345 Now apply Exchange = 12.345/1 = 12.345 Same for amount 54321

Case 2 With Rounding , DECIMAL_SHIFT is On, DECIMAL_SHIFT_BACK is OFF
Rounding is X, Decimal_shift = ‘X’ & decimal_shift_back = ‘ ‘
in the output Rounding will be done
Case 3 Without Rounding , DECIMAL_SHIFT is On, DECIMAL_SHIFT_BACK is On
Rounding = ‘ ‘, Decimal_shift & shift_back is ‘X’
Output
Target currency – Decimal 0 (in table USD has 0 decimal)
DECIMAL_SHIFT  DECIMAL_SHIFT_BACK  
Amount = 1 . From EUR to USD 
Exchange rate as per table EUR = USD * 2 Decimal shift is on so value passed*10 power(2 – num of decimal in source currency) Value passed = 1*10^(20) = 100 Now Exchange = 100 * 2 = 200

Result / 10 power(2 Num of decimal in target currency) Result = 200 / (10^(20)) = 2 
Amount = 1 . From GBP to USD 
Exchange rate as per table GBP = USD * 1.6 Decimal shift is on so value passed*10 power(2 – num of decimal in source currency) Value passed = 1*10^(20) = 100 Now Exchange = 100 * 1.6 = 160

Result = 160 / (10^(20)) = 1.60 
Amount = 1 . From JPY to USD 
Exchange rate as per table JPY = USD / 114.75 Decimal shift is on so value passed*10 power(2 – num of decimal in source currency) Value passed = 1*10^(20) = 100 Now Exchange = 100 / 114.75 = 0.87 
Result = 0.87/(10^(20)) = 0.087 for 2 decimal digit it is 0.01 
Amount = 1 . From USD to USD 
Exchange rate as per table USD = USD * 1 Decimal shift is on so value passed*10 power(2 – num of decimal in source currency) Value passed = 1*10^(20) = 100 Now Exchange = 100 * 1 = 100 

Amount = 12345 . From USDN to USD 
Exchange rate as per table USDN = USD / 1 Decimal shift is on so value passed*10 power(2 – num of decimal in source currency) Value passed = 12345*10^(25) = 12345*10^3 = 12.345 Now Exchange = 12.345/1 = 12.345 Same for amount 54321

Result = 12.35/(10^(20)) = 0.12 Similar for amount 54321 
Case 4 With Rounding , DECIMAL_SHIT is On, DECIMAL_SHIFT_BACK is On
Rounding = ‘X’, decimal_shift & shift_back = ‘X’
I hope above simple example will clear the doubts in using decimal_shift & decimal_shift_back.
good one Ashish, thanks for sharing