Add number conversion

This commit is contained in:
Michał Sałaban 2017-11-25 23:59:32 +01:00
parent bbeb7d85a2
commit 0952fe0893
4 changed files with 27 additions and 0 deletions

9
monero/numbers.py Normal file
View file

@ -0,0 +1,9 @@
from decimal import Decimal
PICONERO = Decimal('0.000000000001')
def to_atomic(amount):
return int(amount * 10**12)
def from_atomic(amount):
return (Decimal(amount) * PICONERO).quantize(PICONERO)

2
tests/__init__.py Normal file
View file

@ -0,0 +1,2 @@
from .address import AddressTestCase, TestnetAddressTestCase
from .numbers import NumbersTestCase

16
tests/numbers.py Normal file
View file

@ -0,0 +1,16 @@
from decimal import Decimal
import unittest
from monero.numbers import to_atomic, from_atomic
class NumbersTestCase(unittest.TestCase):
def test_simple_numbers(self):
self.assertEqual(to_atomic(Decimal('0')), 0)
self.assertEqual(from_atomic(0), Decimal('0'))
self.assertEqual(to_atomic(Decimal('1')), 1000000000000)
self.assertEqual(from_atomic(1000000000000), Decimal('1'))
self.assertEqual(to_atomic(Decimal('0.000000000001')), 1)
self.assertEqual(from_atomic(1), Decimal('0.000000000001'))
def test_rounding(self):
self.assertEqual(to_atomic(Decimal('1.0000000000004')), 1000000000000)