Crate moneta

source ·
Expand description

Currency-safe computations with money amounts.

Money is a special type of quantity. Its unit type is known as Currency.

Money differs from physical quantities mainly in two aspects:

  • Money amounts are discrete. For each currency there is a smallest fraction that can not be split further.

  • The relation between different currencies is not fixed, instead, it varies over time.

This package provides types to deal with these specifics. It is based an the package “quantities” with feature “fpdec” (aliasing “AmountT” to “Decimal”).

Currency is an enumeration of all currencies defined in ISO 4217. In addition, for each currency there is a constant named after the 3-character ISO 4217 code.

Currency implements quantities::Unit, so all operations on units can be applied to Currency. Especially, a Currency instance can be multiplied with an AmountT to create a Money instance.

Example:

let amnt = Amnt!(17.95);
let eur_amnt = amnt * EUR;
assert_eq!(eur_amnt.amount(), amnt);
assert_eq!(eur_amnt.unit(), Currency::EUR);

Money implements trait quantities::Quantity, so all operations on quantities can also be applied to instances of Money. Because there is no fixed relation between currencies, there is no implicit conversion between money amounts of different currencies. Resulting values are always quantized to the smallest fraction defined with the currency.

Example:

let qty = Amnt!(3.2);
let price = Money::new(Amnt!(13.58), EUR);
let total = qty * price;
assert_eq!(total.to_string(), "43.46 EUR");

A conversion factor between two currencies can be defined by using the type ExchangeRate. It is given a unit currency (aka base currency), a unit multiple, a term currency (aka price currency) and a term amount, i.e. the amount in term currency equivalent to unit multiple in unit currency.

Multiplying an amount in some currency with an exchange rate with the same currency as unit currency results in the equivalent amount in term currency. Likewise, dividing an amount in some currency with an exchange rate with the same currency as term currency results in the equivalent amount in unit currency.

Examples:

let usd = Dec!(17.95) * USD;
let rate = ExchangeRate::new(USD, 1, EUR, Dec!(0.98078));
let eur = usd * rate;
assert_eq!(eur.to_string(), "17.61 EUR");
let rate = ExchangeRate::new(HKD, 10, EUR, Dec!(1.187253));
let hkd = eur / rate;
assert_eq!(hkd.to_string(), "148.33 HKD");

Macros

  • Converts a numeric literal to an AmountT.
  • Macro used to convert a number literal into a Decimal.

Structs

  • Represents a decimal number as a coefficient (i128) combined with a value (u8) specifying the number of fractional decimal digits.
  • Basic representation of a conversion factor between two currencies.
  • Represents a money amount, i.e. the combination of a numerical value and a money unit, aka. currency.
  • The ratio between two related quantity values.

Enums

  • Unit of quantity Money.
  • Enum of unit prefixes defined for the System of Units (SI).

Constants

  • UAE Dirham
  • Afghani
  • Lek
  • Armenian Dram
  • Netherlands Antillean Guilder
  • Kwanza
  • Argentine Peso
  • Australian Dollar
  • Aruban Florin
  • Azerbaijan Manat
  • Convertible Mark
  • Barbados Dollar
  • Taka
  • Bulgarian Lev
  • Bahraini Dinar
  • Burundi Franc
  • Bermudian Dollar
  • Brunei Dollar
  • Boliviano
  • Mvdol
  • Brazilian Real
  • Bahamian Dollar
  • Ngultrum
  • Pula
  • Belarusian Ruble
  • Belize Dollar
  • Canadian Dollar
  • Congolese Franc
  • WIR Euro
  • Swiss Franc
  • WIR Franc
  • Unidad de Fomento
  • Chilean Peso
  • Yuan Renminbi
  • Colombian Peso
  • Unidad de Valor Real
  • Costa Rican Colon
  • Peso Convertible
  • Cuban Peso
  • Cabo Verde Escudo
  • Czech Koruna
  • Djibouti Franc
  • Danish Krone
  • Dominican Peso
  • Algerian Dinar
  • Egyptian Pound
  • Nakfa
  • Ethiopian Birr
  • Euro
  • Fiji Dollar
  • Falkland Islands Pound
  • Pound Sterling
  • Lari
  • Ghana Cedi
  • Gibraltar Pound
  • Dalasi
  • Guinean Franc
  • Quetzal
  • Guyana Dollar
  • Hong Kong Dollar
  • Lempira
  • Kuna
  • Gourde
  • Forint
  • Rupiah
  • New Israeli Sheqel
  • Indian Rupee
  • Iraqi Dinar
  • Iranian Rial
  • Iceland Krona
  • Jamaican Dollar
  • Jordanian Dinar
  • Yen
  • Kenyan Shilling
  • Som
  • Riel
  • Comorian Franc
  • North Korean Won
  • Won
  • Kuwaiti Dinar
  • Cayman Islands Dollar
  • Tenge
  • Lao Kip
  • Lebanese Pound
  • Sri Lanka Rupee
  • Liberian Dollar
  • Loti
  • Libyan Dinar
  • Moroccan Dirham
  • Moldovan Leu
  • Malagasy Ariary
  • Denar
  • Kyat
  • Tugrik
  • Pataca
  • Ouguiya
  • Mauritius Rupee
  • Rufiyaa
  • Malawi Kwacha
  • Mexican Peso
  • Mexican Unidad de Inversion (UDI)
  • Malaysian Ringgit
  • Mozambique Metical
  • Namibia Dollar
  • Naira
  • Cordoba Oro
  • Norwegian Krone
  • Nepalese Rupee
  • New Zealand Dollar
  • Rial Omani
  • Balboa
  • Sol
  • Kina
  • Philippine Peso
  • Pakistan Rupee
  • Zloty
  • Guarani
  • Qatari Rial
  • Romanian Leu
  • Serbian Dinar
  • Russian Ruble
  • Rwanda Franc
  • Saudi Riyal
  • Solomon Islands Dollar
  • Seychelles Rupee
  • Sudanese Pound
  • Swedish Krona
  • Singapore Dollar
  • Saint Helena Pound
  • Leone
  • Leone
  • Somali Shilling
  • Surinam Dollar
  • South Sudanese Pound
  • Dobra
  • El Salvador Colon
  • Syrian Pound
  • Lilangeni
  • Baht
  • Somoni
  • Turkmenistan New Manat
  • Tunisian Dinar
  • Pa’anga
  • Turkish Lira
  • Trinidad and Tobago Dollar
  • New Taiwan Dollar
  • Tanzanian Shilling
  • Hryvnia
  • Uganda Shilling
  • US Dollar
  • US Dollar (Next day)
  • Uruguay Peso en Unidades Indexadas (UI)
  • Peso Uruguayo
  • Unidad Previsional
  • Uzbekistan Sum
  • Bolívar Soberano
  • Bolívar Soberano
  • Dong
  • Vatu
  • Tala
  • CFA Franc BEAC
  • East Caribbean Dollar
  • CFA Franc BCEAO
  • CFP Franc
  • Yemeni Rial
  • Rand
  • Zambian Kwacha
  • Zimbabwe Dollar

Traits

  • The abstract type of quantities.
  • The abstract type of units used to define quantities.

Type Definitions

  • Type used for the numerical part of a Quantity.