mirror of
https://git.wownero.com/lza_menace/wownero-python.git
synced 2024-08-15 03:25:25 +00:00
Add number conversion
This commit is contained in:
parent
bbeb7d85a2
commit
0952fe0893
4 changed files with 27 additions and 0 deletions
9
monero/numbers.py
Normal file
9
monero/numbers.py
Normal 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
2
tests/__init__.py
Normal file
|
@ -0,0 +1,2 @@
|
|||
from .address import AddressTestCase, TestnetAddressTestCase
|
||||
from .numbers import NumbersTestCase
|
16
tests/numbers.py
Normal file
16
tests/numbers.py
Normal 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)
|
Loading…
Reference in a new issue