Currencies, Units and Conversion Exits

This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production.

An amount of money is never just a number. The figure 1,234 means very different things in euros and in Japanese yen, because those currencies do not use the same number of decimal places. The same is true of a quantity: ten pieces and ten kilograms are not the same thing. ABAP therefore keeps an amount or a quantity together with a second field that says which currency or which unit it is in, and it uses small conversion routines to turn stored values into what a person expects to read. A newcomer meets this as soon as a claim has a price: the amount looks right in one table and wrong on a screen, or a document number loses its leading zeros. After reading this page you can declare an amount with its currency key, explain why the stored value of an amount can differ from the displayed one, use the ALPHA conversion exit and know where currency and unit conversion are done. The page is written against Standard ABAP release 8.16 and ABAP for Cloud Development release 9.20 (2608). The type rules and the string-template options below apply to both ABAP Cloud and classic Standard ABAP; the older function modules and WRITE additions belong to classic Standard ABAP, which means on-premise systems and the Private Edition, and are not the form to use in ABAP Cloud.

Before you read

Key terms

  • Currency key: a short code such as EUR or JPY, kept in a separate field, that says which currency an amount is in.

  • Reference field: the field a quantity or amount points to, so that the system knows its unit or currency.

  • Packed number: a number type that stores decimal digits exactly, used for amounts and quantities.

  • Conversion exit: a routine attached to a data element that turns a value between its stored form and its display form.

  • Unit of measure: a code such as KG or PC that says what a quantity is counted in.

  • Currency conversion: turning an amount in one currency into another with an exchange rate valid on a given date.

Amounts, quantities and their reference fields

In the ABAP Dictionary a money amount has the built-in data type CURR and a quantity has the type QUAN. Both are stored as packed numbers; an amount is an integer in the smallest unit of the currency, built from all the digits of the field while ignoring the position of the decimal separator. Each of them must point to a reference field of a different kind: a CURR field points to a field of type CUKY, the currency key, and a QUAN field points to a field of type UNIT, the unit of measure. The reference field lives in the same table or structure as the amount. Without it the number carries no meaning, because the same digits would be read under whatever currency or unit a program happened to assume.

flowchart LR A["Amount field, type CURR, packed number"] -- "refers to" --> C["Currency key field, type CUKY, for example EUR"] Q["Quantity field, type QUAN, packed number"] -- "refers to" --> U["Unit field, type UNIT, for example PC"] C -- "decimals looked up for the key" --> T["Currency table, number of decimals per currency"]

For the running example, an expense claim carries a total and its currency, and each item carries a quantity and a unit. In a CDS view or a table definition the link is declared with an annotation, so that tools such as the SAP Fiori user interface can format the amount correctly. The listing shows a view over the two tables with the annotations that name the reference field. It requires an SAP system; not run here.

@AbapCatalog.viewEnhancementCategory: [#NONE]
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Expense claim with currency'
define view entity ZI_ExpClaimAmount
  as select from zexp_claim
{
  key claim_id,
      @Semantics.currencyCode: true
      currency_code,
      @Semantics.amount.currencyCode: 'currency_code'
      total_amount
}

The same pairing exists for quantities with @Semantics.unitOfMeasure: true on the unit field and @Semantics.quantity.unitOfMeasure: '<unit field>' on the quantity, as described in ABAP CDS - Quantity Fields.

Why the stored value can differ from the displayed one

Currencies do not all use two decimal places. By default the number of decimal places of a currency is two, and a currency with a different number must be entered in the Dictionary table TCURX, which holds the number of decimals explicitly. The Dictionary does not give each currency its own field format. A CURR field normally keeps two decimal places (two is the default when the data element is created), and the real number of decimals of the currency named in the key is applied when the value is shown or entered. The result is that the stored digits must be shifted whenever a currency has a number of decimals other than two.

With d as the number of decimals the currency really has, and a CURR field kept with the default two decimals, the relation between the value held in the field and the amount a person reads is:

For a currency with two decimals the factor is 1 and nothing changes. For yen, with d = 0, the factor is , so the stored value 12.34 stands for 1,234 yen. For a currency with three decimals the factor is , so a stored 12.34 stands for 1.234. The practical rule for programs is that an amount must never be shown, compared with an amount in another currency or copied to a field of another type without its currency key travelling with it. The CURRENCY addition of WRITE and the CURRENCY option of a string template do the shift for you when you name the currency.

TYPES ty_amount TYPE p LENGTH 8 DECIMALS 2.

DATA stored   TYPE ty_amount VALUE '12.34'.
DATA currency TYPE c LENGTH 5 VALUE 'JPY'.

" CURRENCY tells the template which currency's decimals apply
DATA(text) = |{ stored CURRENCY = currency }|.

This example requires an SAP system; not run here, because the currency decimals come from the system tables TCURC and TCURX. For a packed number held in a data object the template ignores the decimals of its type and places the decimal separator among the digits as the currency demands. With the key EUR the template yields 12.34. With a currency that the table lists with no decimals, such as JPY, it yields 1234, because a template does not insert thousands separators.

Showing an amount with its currency key

The example of this page is an amount display for a claim: the number is formatted with the right decimals and the currency key is printed next to it. The listing is a small class method that builds the text for one claim. It requires an SAP system; not run here, because the currency decimals come from the system tables.

CLASS zcl_claim_amount_text DEFINITION PUBLIC FINAL CREATE PUBLIC.
  PUBLIC SECTION.
    CLASS-METHODS format
      IMPORTING amount          TYPE zexp_claim-total_amount
                currency        TYPE zexp_claim-currency_code
      RETURNING VALUE(result)   TYPE string.
ENDCLASS.

CLASS zcl_claim_amount_text IMPLEMENTATION.
  METHOD format.
    result = |{ amount CURRENCY = currency } { currency }|.
  ENDMETHOD.
ENDCLASS.

Called with 12.34 and EUR it returns 12.34 EUR, and with the same stored value and JPY it returns 1234 JPY.

Conversion exits

A value can be stored in one form and shown in another. A document number is the usual case: it is stored as ten characters padded with zeros on the left, such as 0000004711, but a person types and reads 4711. A conversion exit is a small routine assigned to a type in the ABAP Dictionary that does this translation in both directions. The exit named ALPHA is the best known. On input it pads a string of digits with zeros on the left up to the field length, and a string that contains other characters is not padded with zeros. On output it removes the leading zeros of a string of digits. Fields that carry an exit are converted when they are shown on a dynpro or written with WRITE; in other program code you ask for the conversion yourself.

flowchart LR I["Typed by a person: 4711"] -- "ALPHA input" --> S["Stored: 0000004711"] S -- "ALPHA output" --> O["Shown to a person: 4711"]

Both ABAP Cloud and classic ABAP can ask for the exit in a string template, for operands of type string, c or n. The option ALPHA = IN adds the zeros and ALPHA = OUT removes them. The example uses a claim number; it runs in the local playground, where claim_id becomes 0000004711 and shown becomes 4711.

DATA claim_id TYPE c LENGTH 10.

" Input direction: pad with zeros on the left
claim_id = |{ '4711' ALPHA = IN WIDTH = 10 }|.

" Output direction: remove the leading zeros again
DATA(shown) = |{ claim_id ALPHA = OUT }|.

Other exits exist for other formats. A program should not hard-code these differences: it should use the exit that the Dictionary type names.

Converting units and currencies

Converting a value is a different task from formatting it. Converting a quantity from kilograms to grams, or an amount from euros to dollars, changes the number. The two conversions need data that the program does not own. Unit conversion needs the ratio between the two units, kept in the unit tables whose rules are maintained in transaction CUNI. Currency conversion needs a date and the exchange rates that are maintained for it, which can be maintained in transaction OB08.

  • In a CDS view entity the functions UNIT_CONVERSION and CURRENCY_CONVERSION do the work inside the database query, so a report can read amounts already converted to a target currency at a given date. CURRENCY_CONVERSION takes the amount, the source and target currency, an exchange rate date and, optionally, an exchange rate type. The documentation also states that it assumes two decimal places for currency fields.

  • In ABAP code, ABAP Cloud development may call only released objects. Which conversion classes or interfaces are released for your release is shown in ADT, so look them up there instead of calling a function module that you find in an older program.

  • Classic ABAP programs of older systems often convert through function modules. This page names none: look them up in the system’s own function library and check that they suit the release you work on.

Whichever route you take, an amount stays attached to its currency key at every step. The converted amount belongs to the target currency key that you passed.

Older material you may meet

Many older ABAP programs and older books handle money and unit formats with tools that current ABAP replaces.

  • Declarations with DATA ... TYPE p DECIMALS 2 and a separate field for the currency key work as before, but current code often uses inline declarations such as DATA(wa) where older code declared every variable up front.

  • Output was written with WRITE amount CURRENCY currency, WRITE quantity UNIT unit and WRITE ... USING EDIT MASK '==ALPHA'. Those additions belong to the classic WRITE statement. A string template with the CURRENCY and ALPHA options does the same work for amounts and digit strings and is the form to use in new code.

  • The conversion exit was called through two function modules, one for each direction (CONVERSION_EXIT_ALPHA_INPUT and CONVERSION_EXIT_ALPHA_OUTPUT). The template option has the same function; you will find the modules in old programs.

  • The shift of the decimals was often written by hand, multiplying by a power of ten after a database read. Reading the decimals from the system tables or letting the template do it avoids the error that appears the day a currency with another number of decimals is added.

Summary

  • An amount is a packed number of type CURR that points to a currency key of type CUKY; a quantity is a packed number of type QUAN that points to a unit of type UNIT.

  • A CURR field normally keeps two decimals; the real decimals of the currency come from the system table TCURX, and the displayed amount equals the stored value times .

  • Pass the currency with the amount to templates and output, so that the right decimals are applied.

  • A conversion exit such as ALPHA turns a value between its stored and its displayed form; templates ask for it with ALPHA = IN or ALPHA = OUT.

  • Unit and currency conversion change the number itself and need unit ratios or exchange rates; use the CDS functions or the conversion APIs that are released for your release.