Ansicht
Dokumentation

ABENCDS_CONV_FUNC_UNIT_CURR_V2 - CDS CONV FUNC UNIT CURR V2

ABENCDS_CONV_FUNC_UNIT_CURR_V2 - CDS CONV FUNC UNIT CURR V2

PERFORM Short Reference   General Data in Customer Master  
This documentation is copyright by SAP AG.
SAP E-Book

- CDS View Entity, Unit and Currency Conversion Functions

... UNIT_CONVERSION( p1 => a1, p2 => a2, ... )
  $| CURRENCY_CONVERSION( p1 => a1, p2 => a2, ... )
  $| GET_NUMERIC_VALUE( arg )
  $| CURR_TO_DECFLOAT_AMOUNT( arg ) ...


Variants:

1. ... UNIT_CONVERSION( p1 => a1, p2 => a2, ... )

2. ... CURRENCY_CONVERSION( p1 => a1, p2 => a2, ... )

3. ... GET_NUMERIC_VALUE( arg )

4. ... CURR_TO_DECFLOAT_AMOUNT( arg )

Effect

Functions for converting between units and between currencies in a CDS view entity.

The functions UNIT_CONVERSION and CURRENCY_CONVERSION have keyword parameters p1, p2, ... (some of which are optional), to which actual parameters a1, a2, ... can or must be assigned when called using =>.

The functions GET_NUMERIC_VALUE and CURR_TO_DECFLOAT_AMOUNT each have one mandatory positional parameter arg.

Note

The function DECIMAL_SHIFT is currently not supported in CDS view entities.

Variant 1

... UNIT_CONVERSION( p1 => a1, p2 => a2, ... )


Effect

The function UNIT_CONVERSION performs a unit conversion for the value passed to the formal parameter quantity.

The unit conversion is performed on the basis of the client-dependent rules saved in transaction CUNI and in the DDIC database tables T006... of the package SZME.

The table below shows the formal parameters p1, p2, ... and their meaning.

Formal Parameter Optional Meaning Data Type Actual Parameter
quantity - Inbound value QUAN, DEC, INT1, INT2, INT4, DECFLOAT16, DECFLOAT34, FLTP Literal, field of a data source, parameter, expression, path expression
source_unit - Source currency from column MSEHI of DDIC database table T006 UNIT Field of a data source, parameter, case distinction, cast expression, path expression
target_unit - Target unit from column MSEHI of DDIC database table T006 UNIT Field of a data source, parameter, case distinction, cast expression, path expression
client X, - Client whose rules are used to perform the unit conversion. Since client handling takes place implicitly for CDS view entities, it is not possible to explicitly specify any client column as actual parameter. For client-dependent sources, there are two options: \lbr - either, the parameter can be left out and the client column is implicitly added, or \lbr - the actual parameter can be specified as expression, literal, or session variable (but not as field of a data source). CLNT Expression, literal, session variable
error_handling X Error handling. \lbr 'FAIL_ON_ERROR': an error raises an exception (default) \lbr 'SET_TO_NULL': the result is set to the null value \lbr '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 has data type QUAN, DEC, INT1, INT2, INT4, or FLTP, the result has 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.

Note

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.

Example

The following CDS view entity calls a unit conversion in the SELECT list for the column DEC3 of the DDIC database table DEMO_EXPRESSIONS. The source unit is specified using a typed literal. The target unit must be passed as a parameter. In the event of an error, for example if a conversion between the entered units is impossible, the result is set to null.

The program DEMO_CDS_UNIT_CONVERSION_VE accesses the view in a SELECT statement. The target unit is passed to the respective formal parameter. 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.

Variant 2

... CURRENCY_CONVERSION( p1 => a1, p2 => a2, ... )


Effect

The function CURRENCY_CONVERSION performs a currency conversion for the value passed to the formal parameter amount. The result has the data type CURR or DECFLOAT34 with the same technical attributes 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. These rules can be edited using transaction OB08.

The table below shows the formal parameters p1, p2, ... and their meaning.

Formal Parameter Optional Meaning Data Type Actual Parameter
amount - Inbound value CURR, DECFLOAT34 Field of a data source, parameter, expression, path expression
source_currency - Source currency from column WAERS of DDIC database table TCURC CUKY Field of a data source, cast expression, path expression, parameter
target_currency - Target currency from column WAERS of DDIC database table TCURC CUKY Field of a data source, cast expression, path expression, parameter
exchange_rate_date - Exchange rate date for column GDATU of DDIC database table TCURR DATS Field of a data source, literal, parameter, path expression.
exchange_rate_type X Exchange rate type from column KURST of DDIC database table TCURR, default value: 'M' CHAR with length 4 Literal, parameter
client X, - Client whose rules are used to perform the currency conversion. Since client handling takes place implicitly for CDS view entities, it is not possible to explicitly specify any client column as actual parameter. For client-dependent sources, there are two options: \lbr - either, the parameter can be left out and the client column is implicitly added, or \lbr - the actual parameter can be specified as expression, literal, or session variable (but not as field of a data source). CLNT Expression, literal, session variable
round X If 'X' (default value), the intermediate result of the conversion is rounded to the end result using commercial rounding; else it is truncated CHAR Literal
decimal_shift X This 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_back X This 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). CHAR Literal
error_handling X Error 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

For the input parameters round, decimal_shift, and decimal_shift_back, it is also possible to specify the literals 'true' or 'false' (not case-sensitive). Internally, these literals are handled like the values 'X' or ' '.

Handling the Decimal Places

  • The inbound value 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 after the conversion.
  • After the conversion, the result is divided by 10 to the power of the number of decimal places of the target currency.

Note

  • The conversion is performed on the database, which means that part of the calculation takes place using different rounding rules from ABAP. The results might differ from the results returned 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.

Example

The following CDS view entity performs 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, for example, when a currency is not available, the result is set to zero.

The program DEMO_CDS_CURRENCY_CONV_VE accesses the view in a SELECT statement, passing the target currency. 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.

Example

In program DEMO_CDS_FUNC_CURR_CONV_VE, the above CDS view entity is used in a subquery of an ABAP SQL MODIFY statement.



Variant 3

... GET_NUMERIC_VALUE( arg )


Effect

GET_NUMERIC_VALUE returns the numeric value of a currency or quantity field without its currency or unit key. arg must be a field of a data source. No other elementary operand, expression, or function is possible. The argument arg can have any data type that supports reference annotations, namely: INT1, INT2, INT4, DEC, FLTP, DECFLOAT16, DECFLOAT34, CURR, or QUAN. The result of the function GET_NUMERIC_VALUE has data type DECFLOAT34 and it has no reference information assigned. That means that no currency or unit key is assigned.

Notes

If arg has data type CURR, an implicit decimal shift takes place.

Example

The following CDS view entity applies the function GET_NUMERIC_VALUE to calculate the number of bookings. It divides the total value of current bookings (field paymentsum) by the price of a flight (field price). This would not be possible without the function GET_NUMERIC_VALUE, since elements of data type CURR are not allowed in arithmetic expressions.

Details on the elements:

  • paymentsum has data type CURR, a reference annotation that assigns a currency key, and it contains the total value of current bookings.
  • price has data type CURR, a reference annotation that assigns a currency key, and it contains the price of a flight.
  • number_of_bookings has data type DECFLOAT34 and it has no currency key assigned.

The program DEMO_CDS_GET_NUMERIC_VALUE accesses the view and performs the following steps:

  • It displays the element annotations of price. One of them is the Semantics.amount.currencyCode annotation that assigns a currency key.
  • It displays the element annotations of number_of_bookings. This field doesn't have any annotation.
  • It displays the structure of the CDS view entity. The element number_of_bookings has type e, which corresponds to the built-in DDIC type DECFLOAT34.





Variant 4

... CURR_TO_DECFLOAT_AMOUNT( arg )


Effect

The function CURR_TO_DECFLOAT_AMOUNT converts a currency field of data type CURR into a currency field of data type DECFLOAT34. arg must have data type CURR and a currency key must be assigned using the annotation Semantics.amount.currencyCode. The result has the data type DECFLOAT34 and exactly the same currency key that is assigned to arg must be assigned.

Notes

This functions performs an implicit decimal shift.

Example

The following CDS view entity demonstrates the CURR_TO_DECFLOAT_AMOUNT function. Field curr10_4 is converted from data type CURR to data type DECFLOAT34. The source and the target field require a currency key assignment.

The program DEMO_CDS_CURR_TO_DEC accesses the view and performs the following steps:

  • It displays all element annotations of the view DEMO_CDS_CURR_TO_DEC. Both source and target field have a currency key assigned.
  • It evaluates the components of the CDS view entity. The element curr_conv has type e, which corresponds to the built-in DDIC type DECFLOAT34.






Addresses (Business Address Services)   ROGBILLS - Synchronize billing plans  
This documentation is copyright by SAP AG.

Length: 32261 Date: 20240420 Time: 070208     sap01-206 ( 333 ms )