wownero-python/monero/numbers.py

31 lines
896 B
Python
Raw Normal View History

2017-11-25 22:59:32 +00:00
from decimal import Decimal
2017-12-27 00:49:59 +00:00
import sys
2017-11-25 22:59:32 +00:00
PICONERO = Decimal('0.000000000001')
2017-12-27 00:49:59 +00:00
if sys.version_info < (3,):
_integer_types = (int, long,)
else:
_integer_types = (int,)
2017-11-25 22:59:32 +00:00
def to_atomic(amount):
2017-11-29 03:38:29 +00:00
"""Convert Monero decimal to atomic integer of piconero."""
2017-11-25 22:59:32 +00:00
return int(amount * 10**12)
def from_atomic(amount):
2017-11-29 03:38:29 +00:00
"""Convert atomic integer of piconero to Monero decimal."""
2017-11-25 22:59:32 +00:00
return (Decimal(amount) * PICONERO).quantize(PICONERO)
2017-11-29 03:38:29 +00:00
def as_monero(amount):
"""Return the amount rounded to maximal Monero precision."""
return Decimal(amount).quantize(PICONERO)
2017-12-27 00:49:59 +00:00
def payment_id_as_int(payment_id):
if isinstance(payment_id, (bytes, str)):
payment_id = int(payment_id, 16)
elif not isinstance(payment_id, _integer_types):
raise TypeError("payment_id must be either int or hexadecimal str or bytes, "
"is %r" % payment_id)
return payment_id