Expression Pattern Language (eXPL)

eXPL Currency

Curreny type in eXPL is a natural choice for representing and transacting in financial amounts. The type is an extension of decimal type with the following features:

  • The locale, a combination of language and country, can be specified in the currency declaration
  • The curreny type will automatically convert amounts represented in text to corresponding decimal values
  • The type of rounding performed on multiplication and division suits financial transactions

Format

A currency declaration has format:

currency [ (locale) ] name.

The optional locale can be just an ISO 3166 alpha-2 country code such as "US" for the USA. However, some countries have more than one currency format according to language, so an ISO 639 alpha-2 language code can be inserted using underscore '_' or dash '-' as a separator character eg. "fr_LU" for French Luxenburg.

The system default applies when the locale is omitted. The locale can be specified as a string literal eg. currency("US") or as a variable eg currency(country). The latter is useful when multiple currencies are being input from an axiom source as shown in the second example below.

Working with text

A currency type converts a formatted amount to a decimal value. It is flexible about whether the currency symbol is present and also whether or not a space character separates this symbol from the amount digits. This flexibility is particularly useful for the Euro, which has many variations on currency symbol placement and separation. Hence the following Geman Euro amounts are treated identically:

12.345,67 €
12.345,67€
€ 12.345,67
€12.345,67
12.345,67

Going the other way, a currency type can format an amount according the locale by using the format modifier eg. when a variable declared as a German Euro currency variable named "total" has value 1,234,56, then the formatted value format(total) is "12.345,67 EUR". Note that the currency code is substituted for the currency symbol to help avoid character encoding issues.

Single Currency Example

This script demonstrates use of currency type to perform a Goods and Services Tax calculation and format the resulting amount with currency code. The item price is represented as a string literal showing that currency type automatically performs conversion of text to decimal.

// Price of item as formatted string
axiom item() {"$1234.56"};
// Create an Australian currency variable named 'amount'
template charge(currency("AU") amount);
// Calculator to add Goods and Services Tax
calc charge_plus_gst(
  currency("AU") total = amount * 1.1,
  string total_text = "Total + gst: " + format(total));
query item_query(item : charge) >> calc(charge_plus_gst);

The tutorial12 SingleCurrency sample runs this query. The solution is:

Total + gst: AUD1,358.02

Multi Currency Example

This script demonstrates use of currency type with world currencies. The country is dynamically qualified each round of unification. Like the SingleCurrency example, this performs a Goods and Services Tax calculation and formats the resulting amount with currency code. The item price is represented this time as a double literal with correct number of decimal places for the indicated for the currency.

// Prices of items with country and double literal amount
// axiom price (country, amount)
// {"MY", 9711.02},
// {"QA", 496.19}, ...
include "world_currency.xpl";
calc charge_plus_gst(
  country,
  currency(country) charge = amount,
  currency(country) total = charge * 1.1,
  string total_text = country + " Total + gst: "
    format(total));
list world_list(charge_plus_gst);
query price_query(price : charge_plus_gst);

The tutorial12 MultiCurrency sample runs this query. The first 2 of 104 expected results:

MY Total + gst: MYR10,682.12
QA Total + gst: QAR 545.81