Posted: Sat Sep 08, 2007 7:15 am Post subject: Currency | Валюта
FWOS_CURRENCY_DECIMALS_READ
All the currency amounts are stored in SAP tables as CURR(n,2) (the same as DEC(n,2)). So before any arithmetic operation value should be adjusted using the real decimals number for the given currency (stored in TCURX).
Conversion Rates by type and date are stored in TCURR (+factors). Standard type is M. Date is stored in inverted format (the most recent date has the numerically smallest value). ABAP code to convert dates:
convert date p_date into inverted-date w_date.
convert inverted-date w_date into date p_date.
the only difference between CONVERT_TO_LOCAL_CURRENCY and CONVERT_TO_FOREIGN_CURRENCY seems to be the following:
Foreign currency is TCURR-FCURR (From Currency)
Local Currency is TCURR-TCURR (To Currency)
So result will be slightly different for the both functions (two rates stored in the TCURR: e.g. JPY->USD rate is 0.00880, USD->JPY rate is 122.00000). Better to use CONVERT_TO_LOCAL_CURRENCY, because multiplication is more exact operation than division.
Both conversion functions can return also selected rate and factors
Code:
FORM DETERMINE_DECIMALS USING I_CURR
I_RATE
O_RATE.
* Declare local variables
DATA: L_DEC LIKE TCURX-CURRDEC.
CONVERT_TO_FOREIGN_CURRENCY
Both CONVERT_TO_LOCAL_CURRENCY and CONVERT_TO_FOREIGN_CURRENCY use complicated logic considering contents of the TCURX (Decimal Places in Currencies), TCURR (Exchange Rates), TCURF (Conversion Factors) and other tables, _ conversion rules etc. Often functions return error message if information in the tables is inconsistent or not maintained.
To simple currency conversion can be used direct calculation based on the tables TCURR and TCURX.
See also BAPIs:
Currency (since 4.0A) - to get currencies and decimals
ExchangeRate (since 4.5A) - to get exchange rates and factors for a date
Code:
REPORT Z_CURR_CONV.
data: w_dec1 like tcurx-currdec,
w_amt like ekpo-netwr,
w_rate like tcurr-ukurs,
w_fact1 like tcurr-ffact,
w_fact2 like tcurr-ffact.
parameters:
p_curr1 like tcurc-waers default 'RUB',
p_amt1 like ekpo-netwr default '7895',
p_curr2 like tcurc-waers default 'USD',
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.
CALL FUNCTION 'FWOS_CURRENCY_DECIMALS_READ'
EXPORTING
I_CURRENCY = p_curr1
IMPORTING
E_DECIMALS = w_dec1
EXCEPTIONS
I_CURRENCY_INITIAL = 1
OTHERS = 2.
IF NOT sy-subrc IS INITIAL.
w_dec1 = 2.
ENDIF.
p_amt1 = p_amt1 * 10 ** ( w_dec1 - 2 ).
write:
/ 'after adjusting using currency decimals:',
/ ' from currency', p_amt1 currency p_curr1, p_curr1,
'<-', p_amt1, '(', w_dec1, ')'.
IF NOT sy-subrc IS INITIAL.
write: / 'Conversion to loc.curr. failed:',
p_curr1, '->', p_curr2, 'err.code=', sy-subrc.
ELSE.
write: / 'to Loc.curr:', p_amt1 currency p_curr1, p_curr1, '->',
w_amt currency p_curr2, p_curr2,
'(', w_rate, ')', w_fact1, w_fact2.
ENDIF.
write / 'End Of Report'.
SPELL_AMOUNT - Convert numbers and figures in words convert an amount into words
convert a number into words (CURRENCY=space)
set decimal point in an amount and return number of decimals for the currency (LANGUAGE=space)
IF NOT sy-subrc IS INITIAL.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
endif.
READ_EXCHANGE_RATE - Determine exchange rate from table TCURR
The exchange rate valid on the specified date is read from table TCURR according to the currency key and the exchange rate type. The ratios for the currency units from table TCURF are transferred to the calling program in addition to the exchange rate determined. if exchange rate fixing is defined for the exchange rate type TYPE_OF_RATE, this information is transferred to the calling program.
Code:
REPORT Z_CURRENCY_CONVERSION.
DATA: gd_fcurr TYPE tcurr-fcurr,
gd_tcurr TYPE tcurr-tcurr,
gd_date TYPE sy-datum,
gd_value TYPE i.
PERFORM currency_conversion USING gd_fcurr
gd_tcurr
gd_date
CHANGING gd_value.
* Convert value to Currency value
*&---------------------------------------------------------------------*
*& Form currency_conversion
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_GD_FCURR text
* -->P_GD_TCURR text
* -->P_GD_DATE text
* <--P_GD_VALUE text
*----------------------------------------------------------------------*
FORM currency_conversion USING p_fcurr
p_tcurr
p_date
CHANGING p_value.
DATA: t_er TYPE tcurr-ukurs,
t_ff TYPE tcurr-ffact,
t_lf TYPE tcurr-tfact,
t_vfd TYPE datum,
ld_erate(12) TYPE c.
CURRENCY_AMOUNT_DISPLAY_TO_SAP
Convert currency value from display to SAP
The following code shows how CURRENCY_AMOUNT_DISPLAY_TO_SAP can be used. You pass it a Currecny code(WAERS) and the displayed currency value. It will convert the value so that it can be stored in SAP. Without using this function module the value stored in SAP is not guaranteed to be correct. Ie 28000 JPY is stored within SAP as 280.
Code:
*Data declaration
*------------
* WMTO_S-AMOUNT = Type DEC :: length 15 :: Deciamls 4
PARAMETER: p_curr like TCURC-WAERS, "Display currency
p_disval like WMTO_S-AMOUNT. "Internal Amount
DATA: gd_intval like WMTO_S-AMOUNT. "Display Amount
CALL FUNCTION 'CURRENCY_AMOUNT_DISPLAY_TO_SAP'
EXPORTING
currency = p_curr
amount_display = p_disval
IMPORTING
AMOUNT_INTERNAL = gd_intval
EXCEPTIONS
INTERNAL_ERROR = 1
OTHERS = 2.
IF NOT sy-subrc IS INITIAL.
* You are now able to store the return value into an SAP table.
ENDIF.
write:/(30) 'Value Displayed on screen in SAP:', p_disval,
/(30) 'Currency:', p_curr,
/(30) 'Internal SAP value', gd_intval.
CURRENCY_AMOUNT_SAP_TO_DISPLAY
Convert currency value from SAP to display
The following code shows how CURRENCY_AMOUNT_SAP_TO_DISPLAY can be used. You pass it a Currecny code(WAERS) and an SAP stored currency value. It will convert the value into its correct currecny value. Without using this function module you are not guaranteed to be using the correct value as it is often missing a number of zeroes.
Ie 28000 JPY is stored within SAP as 280.
Code:
* DATA declaration
*-----------------
* WMTO_S-AMOUNT = Type DEC :: length 15 :: Deciamls 4
parameter: p_discur like TCURC-WAERS, "Display currency
p_intval like WMTO_S-AMOUNT. "Internal Amount
DATA: gd_disval like WMTO_S-AMOUNT. "Display Amount
IF sy-subrc EQ 0.
* You are now able to manipulate the returned value.
* Ie Convert it to GBP
* Without using this function module you would only be manipulating the
* SAP stored value, which is often missing a number of zeroes.
* Ie 28000 JPY is stored within SAP as 280.
ENDIF.
************************************************************************
*End-of-selection.
END-OF-SELECTION.
write:/(30) 'Value stored in SAP:', p_intval,
/(30) 'Displayed currency:', p_discur,
/(30) 'Ammount is displayed Currency:', gd_disval.
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.