lbry-sdk/lbrynet/wallet/dewies.py

41 lines
941 B
Python
Raw Normal View History

2018-10-03 22:38:47 +02:00
import re
import textwrap
from torba.constants import COIN
def lbc_to_dewies(lbc):
if isinstance(lbc, str):
result = re.search(r'^(\d{1,10})\.(\d{1,8})$', lbc)
if result is not None:
whole, fractional = result.groups()
return int(whole+fractional.ljust(8, "0"))
raise ValueError(textwrap.dedent(
2018-10-03 23:10:35 +02:00
"""
2018-10-03 22:38:47 +02:00
Decimal inputs require a value in the ones place and in the tenths place
separated by a period. The value provided, '{}', is not of the correct
format.
The following are examples of valid decimal inputs:
1.0
2018-10-03 23:10:35 +02:00
0.001
2018-10-03 22:38:47 +02:00
2.34500
4534.4
2323434.0000
The following are NOT valid:
83
.456
123.
""".format(lbc)
))
def dewies_to_lbc(dewies):
lbc = '{:.8f}'.format(dewies / COIN).rstrip('0')
if lbc.endswith('.'):
return lbc+'0'
else:
return lbc