-
Notifications
You must be signed in to change notification settings - Fork 0
/
currency.py
30 lines (23 loc) · 898 Bytes
/
currency.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import math
from decimal import Decimal, ROUND_05UP
class Currency:
def __init__(self, name, short, code, symbol, prefix, precision):
self.name = name
self.short = short
self.code = code
self.symbol = symbol
self.prefix = prefix
self.precision = precision
self.grain_ratio = Decimal('.%s' % '1'.zfill(precision))
self.grains_per_unit = int(math.pow(10, precision))
def __repr__(self):
return "'%s'" % self.short
def __unicode__(self):
return self.symbol
def to_grains(self, amount):
return int((Decimal(amount).quantize(
self.grain_ratio, ROUND_05UP))*self.grains_per_unit)
EUR = Currency('Euro', 'EUR', 978, u'\u20AC', False, 2)
GBP = Currency('Pound sterling', 'GBP', 826, u'\u00A3', True, 2)
USD = Currency('United States dollar', 'USD', 840, u'$', True, 2)
DEFAULT = EUR