mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-23 00:54:20 +01:00
pylightning: adds msat.to_approx_str() method
Tries to return the approxmost posible string of a Millisatoshi amount using
various unit representations. The function will round to an effective
number of digits. Default: 3.
```
>>> Millisatoshi("100000sat").to_approx_str()
'0.001btc'
>>> Millisatoshi("100msat").to_approx_str()
'0.1sat'
>>> Millisatoshi("10000000sat").to_approx_str()
'0.1btc'
```
This commit is contained in:
committed by
Christian Decker
parent
654e89b5fc
commit
d8599e5f67
@@ -1,6 +1,7 @@
|
|||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
from math import floor, log10
|
||||||
import socket
|
import socket
|
||||||
|
|
||||||
__version__ = "0.0.7.1"
|
__version__ = "0.0.7.1"
|
||||||
@@ -85,6 +86,37 @@ class Millisatoshi:
|
|||||||
else:
|
else:
|
||||||
return '{:.8f}btc'.format(self.to_btc())
|
return '{:.8f}btc'.format(self.to_btc())
|
||||||
|
|
||||||
|
def to_approx_str(self, digits: int = 3):
|
||||||
|
"""Returns the shortmost string using common units representation.
|
||||||
|
|
||||||
|
Rounds to significant `digits`. Default: 3
|
||||||
|
"""
|
||||||
|
round_to_n = lambda x, n: round(x, -int(floor(log10(x))) + (n - 1))
|
||||||
|
result = None
|
||||||
|
|
||||||
|
# we try to increase digits to check if we did loose out on precision
|
||||||
|
# without gaining a shorter string, since this is a rarely used UI
|
||||||
|
# function, performance is not an issue. Adds at least one iteration.
|
||||||
|
while True:
|
||||||
|
# first round everything down to effective digits
|
||||||
|
amount_rounded = round_to_n(self.millisatoshis, digits)
|
||||||
|
# try different units and take shortest resulting normalized string
|
||||||
|
amounts_str = [
|
||||||
|
"%gbtc" % (amount_rounded / 1000 / 10**8),
|
||||||
|
"%gsat" % (amount_rounded / 1000),
|
||||||
|
"%gmsat" % (amount_rounded),
|
||||||
|
]
|
||||||
|
test_result = min(amounts_str, key=len)
|
||||||
|
|
||||||
|
# check result and do another run if necessary
|
||||||
|
if test_result == result:
|
||||||
|
return result
|
||||||
|
elif not result or len(test_result) <= len(result):
|
||||||
|
digits = digits + 1
|
||||||
|
result = test_result
|
||||||
|
else:
|
||||||
|
return result
|
||||||
|
|
||||||
def to_json(self):
|
def to_json(self):
|
||||||
return self.__repr__()
|
return self.__repr__()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user