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
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.
