Skip to content

Instantly share code, notes, and snippets.

@vst
Created December 29, 2015 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vst/bbb1062026ee46681264 to your computer and use it in GitHub Desktop.
Save vst/bbb1062026ee46681264 to your computer and use it in GitHub Desktop.
from enum import Enum
from decimal import Decimal
class Currency:
"""
Defines an ISO 4217 compatible currency object model.
>>> Currency("XXX", "My Currency", 2)
<Currency XXX 2 My Currency>
>>> usd = Currency("USD", "US Dollar", 2)
>>> usd.quantize(Decimal("1.0001"))
Decimal('1.00')
>>> usd.quantize(Decimal("1.005"))
Decimal('1.00')
>>> usd.quantize(Decimal("1.015"))
Decimal('1.02')
>>> usd.quantize(Decimal("1.025"))
Decimal('1.02')
>>> jpy = Currency("JPY", "Japanese Yen", 0)
>>> jpy.quantize(Decimal("1.0000"))
Decimal('1')
>>> jpy.quantize(Decimal("1.5"))
Decimal('2')
>>> jpy.quantize(Decimal("2.5"))
Decimal('2')
"""
def __init__(self, code: str, name: str, decimals: int):
self.code = code
self.name = name
self.decimals = decimals
def __repr__(self) -> str:
return "<Currency {} {} {}>".format(self.code, self.decimals, self.name)
def quantize(self, amount: Decimal) -> Decimal:
"""
Quantizes the decimal amount wrt to currency's minor units fraction. Note that the
[ROUND HALF TO EVEN](https://en.wikipedia.org/wiki/Rounding#Round_half_to_even)
method is used for rounding purposes.
:param amount: The amount to be quantized.
:return: Quantized amount.
"""
## If decimals are less than negative, return as is:
if self.decimals < 0:
return amount
## If no decimals, return base:
if self.decimals == 0:
return amount.quantize(Decimal("0"))
## Construct quantizer and return:
return amount.quantize(Decimal("0.{}".format("".join(["0"] * self.decimals))))
class Currencies(Enum):
"""
Provides a currency object repository.
"""
AED = Currency("AED", "UAE Dirham", 2)
AFN = Currency("AFN", "Afghani", 2)
ALL = Currency("ALL", "Lek", 2)
AMD = Currency("AMD", "Armenian Dram", 2)
ANG = Currency("ANG", "Netherlands Antillean Guilder", 2)
AOA = Currency("AOA", "Kwanza", 2)
ARS = Currency("ARS", "Argentine Peso", 2)
AUD = Currency("AUD", "Australian Dollar", 2)
AWG = Currency("AWG", "Aruban Florin", 2)
AZN = Currency("AZN", "Azerbaijanian Manat", 2)
BAM = Currency("BAM", "Convertible Mark", 2)
BBD = Currency("BBD", "Barbados Dollar", 2)
BDT = Currency("BDT", "Taka", 2)
BGN = Currency("BGN", "Bulgarian Lev", 2)
BHD = Currency("BHD", "Bahraini Dinar", 3)
BIF = Currency("BIF", "Burundi Franc", 0)
BMD = Currency("BMD", "Bermudian Dollar", 2)
BND = Currency("BND", "Brunei Dollar", 2)
BOB = Currency("BOB", "Boliviano", 2)
BOV = Currency("BOV", "Mvdol", 2)
BRL = Currency("BRL", "Brazilian Real", 2)
BSD = Currency("BSD", "Bahamian Dollar", 2)
BTN = Currency("BTN", "Ngultrum", 2)
BWP = Currency("BWP", "Pula", 2)
BYR = Currency("BYR", "Belarussian Ruble", 0)
BZD = Currency("BZD", "Belize Dollar", 2)
CAD = Currency("CAD", "Canadian Dollar", 2)
CDF = Currency("CDF", "Congolese Franc", 2)
CHE = Currency("CHE", "WIR Euro", 2)
CHF = Currency("CHF", "Swiss Franc", 2)
CHW = Currency("CHW", "WIR Franc", 2)
CLF = Currency("CLF", "Unidad de Fomento", 4)
CLP = Currency("CLP", "Chilean Peso", 0)
CNY = Currency("CNY", "Yuan Renminbi", 2)
COP = Currency("COP", "Colombian Peso", 2)
COU = Currency("COU", "Unidad de Valor Real", 2)
CRC = Currency("CRC", "Costa Rican Colon", 2)
CUC = Currency("CUC", "Peso Convertible", 2)
CUP = Currency("CUP", "Cuban Peso", 2)
CVE = Currency("CVE", "Cabo Verde Escudo", 2)
CZK = Currency("CZK", "Czech Koruna", 2)
DJF = Currency("DJF", "Djibouti Franc", 0)
DKK = Currency("DKK", "Danish Krone", 2)
DOP = Currency("DOP", "Dominican Peso", 2)
DZD = Currency("DZD", "Algerian Dinar", 2)
EGP = Currency("EGP", "Egyptian Pound", 2)
ERN = Currency("ERN", "Nakfa", 2)
ETB = Currency("ETB", "Ethiopian Birr", 2)
EUR = Currency("EUR", "Euro", 2)
FJD = Currency("FJD", "Fiji Dollar", 2)
FKP = Currency("FKP", "Falkland Islands Pound", 2)
GBP = Currency("GBP", "Pound Sterling", 2)
GEL = Currency("GEL", "Lari", 2)
GHS = Currency("GHS", "Ghana Cedi", 2)
GIP = Currency("GIP", "Gibraltar Pound", 2)
GMD = Currency("GMD", "Dalasi", 2)
GNF = Currency("GNF", "Guinea Franc", 0)
GTQ = Currency("GTQ", "Quetzal", 2)
GYD = Currency("GYD", "Guyana Dollar", 2)
HKD = Currency("HKD", "Hong Kong Dollar", 2)
HNL = Currency("HNL", "Lempira", 2)
HRK = Currency("HRK", "Kuna", 2)
HTG = Currency("HTG", "Gourde", 2)
HUF = Currency("HUF", "Forint", 2)
IDR = Currency("IDR", "Rupiah", 2)
ILS = Currency("ILS", "New Israeli Sheqel", 2)
INR = Currency("INR", "Indian Rupee", 2)
IQD = Currency("IQD", "Iraqi Dinar", 3)
IRR = Currency("IRR", "Iranian Rial", 2)
ISK = Currency("ISK", "Iceland Krona", 0)
JMD = Currency("JMD", "Jamaican Dollar", 2)
JOD = Currency("JOD", "Jordanian Dinar", 3)
JPY = Currency("JPY", "Yen", 0)
KES = Currency("KES", "Kenyan Shilling", 2)
KGS = Currency("KGS", "Som", 2)
KHR = Currency("KHR", "Riel", 2)
KMF = Currency("KMF", "Comoro Franc", 0)
KPW = Currency("KPW", "North Korean Won", 2)
KRW = Currency("KRW", "Won", 0)
KWD = Currency("KWD", "Kuwaiti Dinar", 3)
KYD = Currency("KYD", "Cayman Islands Dollar", 2)
KZT = Currency("KZT", "Tenge", 2)
LAK = Currency("LAK", "Kip", 2)
LBP = Currency("LBP", "Lebanese Pound", 2)
LKR = Currency("LKR", "Sri Lanka Rupee", 2)
LRD = Currency("LRD", "Liberian Dollar", 2)
LSL = Currency("LSL", "Loti", 2)
LYD = Currency("LYD", "Libyan Dinar", 3)
MAD = Currency("MAD", "Moroccan Dirham", 2)
MDL = Currency("MDL", "Moldovan Leu", 2)
MGA = Currency("MGA", "Malagasy Ariary", 2)
MKD = Currency("MKD", "Denar", 2)
MMK = Currency("MMK", "Kyat", 2)
MNT = Currency("MNT", "Tugrik", 2)
MOP = Currency("MOP", "Pataca", 2)
MRO = Currency("MRO", "Ouguiya", 2)
MUR = Currency("MUR", "Mauritius Rupee", 2)
MVR = Currency("MVR", "Rufiyaa", 2)
MWK = Currency("MWK", "Kwacha", 2)
MXN = Currency("MXN", "Mexican Peso", 2)
MXV = Currency("MXV", "Mexican Unidad de Inversion (UDI)", 2)
MYR = Currency("MYR", "Malaysian Ringgit", 2)
MZN = Currency("MZN", "Mozambique Metical", 2)
NAD = Currency("NAD", "Namibia Dollar", 2)
NGN = Currency("NGN", "Naira", 2)
NIO = Currency("NIO", "Cordoba Oro", 2)
NOK = Currency("NOK", "Norwegian Krone", 2)
NPR = Currency("NPR", "Nepalese Rupee", 2)
NZD = Currency("NZD", "New Zealand Dollar", 2)
OMR = Currency("OMR", "Rial Omani", 3)
PAB = Currency("PAB", "Balboa", 2)
PEN = Currency("PEN", "Nuevo Sol", 2)
PGK = Currency("PGK", "Kina", 2)
PHP = Currency("PHP", "Philippine Peso", 2)
PKR = Currency("PKR", "Pakistan Rupee", 2)
PLN = Currency("PLN", "Zloty", 2)
PYG = Currency("PYG", "Guarani", 0)
QAR = Currency("QAR", "Qatari Rial", 2)
RON = Currency("RON", "Romanian Leu", 2)
RSD = Currency("RSD", "Serbian Dinar", 2)
RUB = Currency("RUB", "Russian Ruble", 2)
RWF = Currency("RWF", "Rwanda Franc", 0)
SAR = Currency("SAR", "Saudi Riyal", 2)
SBD = Currency("SBD", "Solomon Islands Dollar", 2)
SCR = Currency("SCR", "Seychelles Rupee", 2)
SDG = Currency("SDG", "Sudanese Pound", 2)
SEK = Currency("SEK", "Swedish Krona", 2)
SGD = Currency("SGD", "Singapore Dollar", 2)
SHP = Currency("SHP", "Saint Helena Pound", 2)
SLL = Currency("SLL", "Leone", 2)
SOS = Currency("SOS", "Somali Shilling", 2)
SRD = Currency("SRD", "Surinam Dollar", 2)
SSP = Currency("SSP", "South Sudanese Pound", 2)
STD = Currency("STD", "Dobra", 2)
SVC = Currency("SVC", "El Salvador Colon", 2)
SYP = Currency("SYP", "Syrian Pound", 2)
SZL = Currency("SZL", "Lilangeni", 2)
THB = Currency("THB", "Baht", 2)
TJS = Currency("TJS", "Somoni", 2)
TMT = Currency("TMT", "Turkmenistan New Manat", 2)
TND = Currency("TND", "Tunisian Dinar", 3)
TOP = Currency("TOP", "Pa'anga", 2)
TRY = Currency("TRY", "Turkish Lira", 2)
TTD = Currency("TTD", "Trinidad and Tobago Dollar", 2)
TWD = Currency("TWD", "New Taiwan Dollar", 2)
TZS = Currency("TZS", "Tanzanian Shilling", 2)
UAH = Currency("UAH", "Hryvnia", 2)
UGX = Currency("UGX", "Uganda Shilling", 0)
USD = Currency("USD", "US Dollar", 2)
USN = Currency("USN", "US Dollar (Next day)", 2)
UYI = Currency("UYI", "Uruguay Peso en Unidades Indexadas", 0)
UYU = Currency("UYU", "Peso Uruguayo", 2)
UZS = Currency("UZS", "Uzbekistan Sum", 2)
VEF = Currency("VEF", "Bolivar", 2)
VND = Currency("VND", "Dong", 0)
VUV = Currency("VUV", "Vatu", 0)
WST = Currency("WST", "Tala", 2)
XAG = Currency("XAG", "Silver", -1)
XAU = Currency("XAU", "Gold", -1)
XCD = Currency("XCD", "East Caribbean Dollar", 2)
XPD = Currency("XPD", "Palladium", -1)
XPT = Currency("XPT", "Platinum", -1)
XSU = Currency("XSU", "Sucre", -1)
XUA = Currency("XUA", "ADB Unit of Account", -1)
YER = Currency("YER", "Yemeni Rial", 2)
ZAR = Currency("ZAR", "Rand", 2)
ZMW = Currency("ZMW", "Zambian Kwacha", 2)
ZWL = Currency("ZWL", "Zimbabwe Dollar", 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment