This commit is contained in:
Correl Roush 2021-08-06 12:58:50 -04:00
parent d618dea10a
commit 6fc827cc3e

View file

@ -0,0 +1,30 @@
:PROPERTIES:
:ID: 36fa073d-4b34-4b2b-bf96-0994a44df61e
:END:
#+title: Working with currencies
- https://simple.wikipedia.org/wiki/ISO_4217
#+begin_src python :exports both
from decimal import Decimal
import pint
from iso_4217 import define_currency_units
currency_units = define_currency_units(pint.UnitRegistry(non_int_type=Decimal))
def to_base_units(currency: str, amount: int) -> Decimal:
"""Convert an amount in a currency's smallest denomination to its base.
e.g. a USD value in its lowest denomination (cents) as 1999 would convert to
19.99 (dollars).
"""
subunit = currency_units[f'{currency.upper()}s']
return (subunit * amount).to_base_units().magnitude
return to_base_units('USD', 1999)
#+end_src
#+RESULTS: