Posted: Sat Sep 15, 2007 9:10 pm Post subject: Currency Conversion: simpliest form
Code:
REPORT Z_convert_currency_amount.
data w_amt like ekpo-netwr.
parameters:
p_curr1 like tcurc-waers default 'JPY',
p_amt1 like ekpo-netwr default '7895',
p_curr2 like tcurc-waers default 'SGD',
pdate like sy-datum default '20030311'.
end-of-selection.
write:
/ 'Entered:',
/ ' from currency:', p_amt1 currency p_curr1, p_curr1, '<-', p_amt1,
/ ' to currency :', p_curr2,
/ ' on :', pdate.
skip.
perform adjust_currency_decimals using p_curr1
changing p_amt1.
write:
/ 'after adjusting enterd value using currency decimals:',
/ ' from currency:', p_amt1 currency p_curr1, p_curr1.
skip.
perform convert_currency_amount using p_curr1
p_amt1
p_curr2
pdate
changing w_amt.
write: / 'Conversion result:', p_amt1 currency p_curr1, p_curr1, '->',
w_amt currency p_curr2, p_curr2.
skip.
write / 'End Of Report'.
*&---------------------------------------------------------------------*
*& Form adjust_currency_decimals
*&---------------------------------------------------------------------*
* -->P_CURR Currency Code
* <--P_AMT Amount
*----------------------------------------------------------------------*
FORM adjust_currency_decimals USING value(P_CURR) like tcurc-waers
CHANGING P_AMT like ekpo-netwr.
data w_dec like tcurx-currdec.
select single currdec from tcurx into w_dec where currkey = p_curr.
if sy-subrc <> 0.
w_dec = 2.
endif.
p_amt = p_amt * 10 ** ( w_dec - 2 ).
ENDFORM. " adjust_currency_decimals
*&---------------------------------------------------------------------*
*& Form convert_currency_amount
*&---------------------------------------------------------------------*
* -->P_CURR1 Currency From
* -->P_AMT Amount to Be Converted
* -->P_CURR2 Currency To
* -->P_DATE Date for Conversion Rate
* <--P_AMT2 Converted Amount
*----------------------------------------------------------------------*
FORM convert_currency_amount USING value(P_CURR1) like tcurc-waers
value(P_AMT1) like ekpo-netwr
value(P_CURR2) like tcurc-waers
value(P_DATE) like sy-datum
CHANGING P_AMT2 like ekpo-netwr.
data: w_ukurs like tcurr-ukurs,
w_ffact like tcurr-ffact,
w_tfact like tcurr-tfact,
w_d like tcurr-gdatu,
w_date like tcurr-gdatu,
w_dec1 like tcurx-currdec,
w_dec2 like tcurx-currdec.
* Get Decimals for both Currencies
select single currdec from tcurx into w_dec1 where currkey = p_curr1.
if sy-subrc <> 0.
w_dec1 = 2.
endif.
select single currdec from tcurx into w_dec2 where currkey = p_curr2.
if sy-subrc <> 0.
w_dec2 = 2.
endif.
* Select Rate for Currency conversion
* the most recent date has the numerically smallest value
convert date p_date into inverted-date w_date.
select ukurs gdatu ffact tfact from tcurr up to 1 rows
into (w_ukurs, w_d, w_ffact, w_tfact)
where kurst = 'M'
and fcurr = p_curr1
and tcurr = p_curr2
and gdatu >= w_date
order by gdatu ascending.
endselect.
if sy-subrc <> 0.
p_amt2 = 0.
else.
if w_ffact is initial.
w_ffact = 1.
endif.
if w_tfact is initial.
w_tfact = 1.
endif.
p_amt2 = p_amt1 * w_ukurs
* 10 ** ( w_dec2 - w_dec1 ) * w_tfact / w_ffact.
endif.
ENDFORM. " convert_currency_amount
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG. SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG. Every effort is made to ensure content integrity. Use information on this site at your own risk.