SAP CDS CONV FUNC UNIT CURR V1



Get Example source ABAP code based on a different SAP table
  


• CURRENCY_CONVERSION ABAP_CDS_FUNCTION_V1
• UNIT_CONVERSION ABAP_CDS_FUNCTION_V1
• DECIMAL_SHIFT ABAP_CDS_FUNCTION_V1

ABAP_CDS_DDL - DDIC-Based View, Unit and Currency Conversion Functions

ABAP_SYNTAX
... UNIT_CONVERSION( quantity => arg1,
source_unit => arg2,
target_unit => arg3$[,
client => arg4$]$[,
error_handling => arg5$])
$| CURRENCY_CONVERSION(amount => arg1,
source_currency => arg2,
target_currency => arg3,
exchange_rate_date => arg4$[,
exchange_rate_type => arg5$]$[,
client => arg6$]$[,
round => arg7$]$[,
decimal_shift => arg8$]$[,
decimal_shift_back => arg9$]$[,
error_handling => arg10$])
$| DECIMAL_SHIFT( amount => arg1,
currency => arg2$[,
error_handling => arg3$])

ABAP_VARIANTS:
1 ... UNIT_CONVERSION( ... )
2 ... CURRENCY_CONVERSION( ... )
3 ... DECIMAL_SHIFT( ... )

What does it do?
Functions for converting between units and between currencies ABAP_CDS_V1_VIEW . The functions have keyword parameters quantity, amount, ... (some of which are optional), to which actual parameters arg1 , arg2, ... can or must be assigned when called using => .

ABAP_VARIANT_1 ... UNIT_CONVERSION( ... )

What does it do?
The function UNIT_CONVERSION performs a unit conversion for the value passed to the keyword parameter quantity.
The unit conversion is performed on the basis of the client-dependent rules saved in
BEGIN_SECTION VERSION 5 OUT transaction CUNI and in
END_SECTION VERSION 5 OUT the DDIC database tables T006... of the package SZME.
The table below shows all available keyword parameters and their meaning. Formal ParameterOptionalMeaning Data Type Actual Parameter arg
quantity-Inbound value QUAN, DEC, INT1, INT2, INT4, FLTPField of a data source, parameter
source_unit-Source currency from column MSEHI of DDIC database table T006 UNITField of a data source, expression that returns a suitable data type, parameter
target_unit-Target unit from column MSEHI of DDIC database table T006 UNITField of a data source, expression that returns a suitable data type, parameter
clientX, -Client whose rules are used to perform the unit conversion. Optional, if the current data source is client-dependent. Default: Content of the client column of the current rowCLNT Field of a data source, literal, parameter
error_handlingXError handling. If FAIL_ON_ERROR (default value), an error raises an exception; if SET_TO_NULL , the result is set to the null value ; if KEEP_UNCONVERTED, the source value is not changed. CHAR with length 20 Literal
The data type of the result depends on the data type of the formal parameter quantity:
If quantity does not have a decimal floating point type, the result has the data type QUAN with the length 31 and 14 decimal places.
If quantity has data type DECFLOAT16, the result has data type DECFLOAT16.
If quantity has data type DECFLOAT34, the result has data type DECFLOAT34.



Latest notes:

The precision of the result of the unit conversion depends on the database platform. The highest precision is achieved on databases that support decimal floating point numbers. Due to rounding, the result can be different from a unit conversion performed using ABAP methods, such as a standard function module.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
The following CDS view calls a unit conversion in the SELECT list for the column DEC3 of the DDIC database table DEMO_EXPRESSIONS. The source unit is a literal that is cast to the required type. The target unit must be passed as a parameter. In the event of an error, e.g. if a conversion between the entered units is impossible, the result is reset to null.
DDLS DEMO_CDS_UNIT_CONVERSION
The class CL_DEMO_CDS_UNIT_CONVERSION accesses the view in a SELECT statement. Here, the target unit is passed to the parameter in question. As a comparison, the same conversion is also performed using the function module UNIT_CONVERSION_SIMPLE. As prerequisite for the example, the units and their conversion rules must be available in the corresponding DDIC database tables.
ABAP_EXAMPLE_END

ABAP_VARIANT_2 ... CURRENCY_CONVERSION( ... )

What does it do?
The function CURRENCY_CONVERSION performs a currency conversion for the value passed to the keyword parameter amount. The result has the data type CURR or DECFLOAT34 with the same technical properties as the actual parameter passed to amount . The currency conversion is performed on the basis of the client-dependent rules saved in the DDIC database tables TCUR... of package SFIB.
BEGIN_SECTION VERSION 5 OUT These rules can be edited using transaction OB08 .
END_SECTION VERSION 5 OUT
The table below shows all available keyword parameters and their meaning. Formal ParameterOptionalMeaning Data Type Actual Parameter arg
amount-Inbound value CURR, DECFLOAT34Field of a data source, parameter
source_currency -Source currency from column WAERS of DDIC database table TCURC CUKYField of a data source, literal, parameter
target_currency-Target currency from column WAERS of DDIC database table TCURC CUKYField of a data source, literal, parameter
exchange_rate_date-Exchange rate date for column GDATU of DDIC database table TCURR DATSField of a data source, literal, parameter
exchange_rate_typeXExchange rate type from column KURST of DDIC database table TCURR , default value: MCHAR with length 4Literal, parameter
clientX, -Client whose rules are used to perform the currency conversion. Optional, if the current data source is client-dependent. Default: Content of the client column of the current rowCLNT Field of a data source, literal, parameter
roundXIf X (default value), the intermediate result of the conversion is rounded to the end result using commercial rounding; else it is truncatedCHAR Literal
decimal_shiftXThis formal parameter can only be used with data type CURR for amount. lbr lbr If X (default value), the decimal places of the source value are moved as specified by the decimal places of the source currency (see below). CHAR Literal
decimal_shift_backXThis formal parameter can only be used with data type CURR for amount. lbr lbr If X (default value), the decimal places of the result are moved as specified by the decimal places of the target currency (see below). CHARLiteral
error_handlingXError handling. If FAIL_ON_ERROR (default value), an error raises an exception; if SET_TO_NULL , the result is reset to the null value ; if KEEP_UNCONVERTED, the source value is not changed. CHAR with length 20 Literal
The input parameters round, decimal_shift, and decimal_shift_back accept the literals 'true', 'false', 'X', or ' '. #cdsboolean.true and #cdsboolean.false with the domain prefix CDSBOOLEAN are also possible. Internally, these literals are handled like the values X or blank.
ABAP_CAUTION The literal values 'true' and 'false' (with and without domain prefix) are case-sensitive. Uppercase letters, such as #cdsboolean.TRUE and #cdsboolean.FALSE, are not valid input. No error message occurs, but these values are not considered valid input and unexpected behavior may occur.
Handling the Decimal Places
The value passed is rounded to two decimal places before it is converted.
Before the conversion, the value passed is multiplied by 10 to the power of the number of decimal places of the source currency.
If the value X or true 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.
If the value X or true 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.
After the conversion, the result is divided by 10 to the power of the number of decimal places of the target currency.



Latest notes:

The conversion is performed on the database, which means that part of the calculation takes place using different rounding rules from ABAP. No matter how the conversion is made, the same results cannot be expected as when using standard function modules for currency conversion, since these modules are generally less precise and round the intermediate results accordingly.
The parameter decimal_shift is intended to set the source value to the number of decimal places of the source currency before the conversion. This assumes that its technical type, CURR, has two decimal places as usual. The parameter decimal_shift_back is intended to perform the reverse operation.
If the technical type CURR of the source value does not have two decimal places, the behavior of the CURRENCY_CONVERSION function can be unexpected.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
The following CDS view calls a currency conversion in the SELECT list for the column AMOUNT of the DDIC database table DEMO_PRICES. The target currency must be passed as a parameter. In the event of an error, e.g. when a currency is not available, the result is reset to zero.
DDLS DEMO_CDS_CURR_CONV
The class CL_DEMO_CDS_CURRENCY_CNVRSN accesses the view in a SELECT statement. The target currency is passed here. As a comparison, the same conversion is also performed using the function module CONVERT_TO_LOCAL_CURRENCY. As prerequisite for the example, the currencies and conversion rules must be available in the corresponding DDIC database tables.
ABAP_EXAMPLE_END

ABAP_EXAMPLE_VX5
In class CL_DEMO_CDS_FUNC_CURR_CONV , the above CDS view is used in a subquery of an ABAP SQL MODIFY statement.
ABEXA 01465
ABAP_EXAMPLE_END

ABAP_VARIANT_3 ... DECIMAL_SHIFT( ... )

What does it do?
The function DECIMAL_SHIFT sets the decimal separator of the value that is passed to the keyword parameter amount in accordance with a currency. The result has the data type CURR with the length 31 and 14 decimal places. Its value is produced by multiplying the input parameter rounded to two decimal places by 10 to the power of two minus the decimal places defined by the currency passed.
Possible currencies and their decimal places are based on the database tables TCUR... of the package SFIB . Formal ParameterOptionalMeaning Data Type Actual Parameter arg
amount-Inbound value CURRField of a data source, parameter
currency -Currency from column WAERS of DDIC database table TCURC; the associated decimal places determined from the column CURRDEC of TCURX CUKYField of a data source, literal, parameter
error_handlingXError handling. If FAIL_ON_ERROR (default value), an error raises an exception; if SET_TO_NULL , the result is reset to the null value ; if KEEP_UNCONVERTED, the source value is not changed. CHAR with length 20 Literal



Latest notes:

If the type of the actual parameter passed to amount has two decimal places, its value is set to the number of decimal places of the passed currency.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
In the SELECT list, the following CDS view sets the decimal separator for column AMOUNT of DDIC database table DEMO_PRICES to between 0 and 5, according to the relevant currency. The column AMOUNT has two decimal places, which means that the decimal places are determined directly by the passed currencies. In the event of an error, e.g. when a currency is not available, the result is reset to zero.
DDLS DEMO_CDS_DECIMAL_SHIFT
The class CL_DEMO_CDS_DECIMAL_SHIFT accesses the view in a SELECT statement. As prerequisite for the example, the currencies and their number of decimal places must be available in the corresponding DDIC database tables.
ABAP_EXAMPLE_END