updates
This commit is contained in:
parent
d618dea10a
commit
6fc827cc3e
1 changed files with 30 additions and 0 deletions
30
20210806111448-working_with_currencies.org
Normal file
30
20210806111448-working_with_currencies.org
Normal 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:
|
Loading…
Reference in a new issue