Fixed issue with thousands separator for better readability (#7427)
util.format_satoshis: introduce new option to add thousands separators
This commit is contained in:
@@ -659,7 +659,7 @@ class SimpleConfig(Logger):
|
|||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def format_amount(self, x, is_diff=False, whitespaces=False):
|
def format_amount(self, x, is_diff=False, whitespaces=False, add_thousands_sep=True):
|
||||||
return format_satoshis(
|
return format_satoshis(
|
||||||
x,
|
x,
|
||||||
num_zeros=self.num_zeros,
|
num_zeros=self.num_zeros,
|
||||||
@@ -667,6 +667,7 @@ class SimpleConfig(Logger):
|
|||||||
is_diff=is_diff,
|
is_diff=is_diff,
|
||||||
whitespaces=whitespaces,
|
whitespaces=whitespaces,
|
||||||
precision=self.amt_precision_post_satoshi,
|
precision=self.amt_precision_post_satoshi,
|
||||||
|
add_thousands_sep=add_thousands_sep,
|
||||||
)
|
)
|
||||||
|
|
||||||
def format_amount_and_units(self, amount):
|
def format_amount_and_units(self, amount):
|
||||||
|
|||||||
@@ -70,6 +70,14 @@ class TestUtil(ElectrumTestCase):
|
|||||||
def test_format_satoshis_diff_negative(self):
|
def test_format_satoshis_diff_negative(self):
|
||||||
self.assertEqual("-0.00001234", format_satoshis(-1234, is_diff=True))
|
self.assertEqual("-0.00001234", format_satoshis(-1234, is_diff=True))
|
||||||
self.assertEqual("-456789.00001234", format_satoshis(-45678900001234, is_diff=True))
|
self.assertEqual("-456789.00001234", format_satoshis(-45678900001234, is_diff=True))
|
||||||
|
|
||||||
|
def test_format_satoshis_add_thousands_sep(self):
|
||||||
|
self.assertEqual("178 890 000.", format_satoshis(Decimal(178890000), decimal_point=0, add_thousands_sep=True))
|
||||||
|
self.assertEqual("458 312.757 48", format_satoshis(Decimal("45831275.748"), decimal_point=2, add_thousands_sep=True, precision=5))
|
||||||
|
self.assertEqual("+4 583 127.574 8", format_satoshis(Decimal("45831275.748"), decimal_point=1, is_diff=True, add_thousands_sep=True, precision=4))
|
||||||
|
self.assertEqual("+456 789 112.004 56", format_satoshis(Decimal("456789112.00456"), decimal_point=0, is_diff=True, add_thousands_sep=True, precision=5))
|
||||||
|
self.assertEqual("-0.000 012 34", format_satoshis(-1234, is_diff=True, add_thousands_sep=True))
|
||||||
|
self.assertEqual("-456 789.000 012 34", format_satoshis(-45678900001234, is_diff=True, add_thousands_sep=True))
|
||||||
|
|
||||||
def test_format_satoshis_plain(self):
|
def test_format_satoshis_plain(self):
|
||||||
self.assertEqual("0.00001234", format_satoshis_plain(1234))
|
self.assertEqual("0.00001234", format_satoshis_plain(1234))
|
||||||
|
|||||||
@@ -659,6 +659,7 @@ def format_satoshis(
|
|||||||
precision: int = 0, # extra digits after satoshi precision
|
precision: int = 0, # extra digits after satoshi precision
|
||||||
is_diff: bool = False, # if True, enforce a leading sign (+/-)
|
is_diff: bool = False, # if True, enforce a leading sign (+/-)
|
||||||
whitespaces: bool = False, # if True, add whitespaces, to align numbers in a column
|
whitespaces: bool = False, # if True, add whitespaces, to align numbers in a column
|
||||||
|
add_thousands_sep: bool = False, # if True, add whitespaces, for better readability of the numbers
|
||||||
) -> str:
|
) -> str:
|
||||||
if x is None:
|
if x is None:
|
||||||
return 'unknown'
|
return 'unknown'
|
||||||
@@ -681,6 +682,14 @@ def format_satoshis(
|
|||||||
integer_part, fract_part = result.split(".")
|
integer_part, fract_part = result.split(".")
|
||||||
if len(fract_part) < num_zeros:
|
if len(fract_part) < num_zeros:
|
||||||
fract_part += "0" * (num_zeros - len(fract_part))
|
fract_part += "0" * (num_zeros - len(fract_part))
|
||||||
|
# add whitespaces as thousands' separator for better readability of numbers
|
||||||
|
if add_thousands_sep:
|
||||||
|
sign = integer_part[0] if integer_part[0] in ("+", "-") else ""
|
||||||
|
if sign == "-":
|
||||||
|
integer_part = integer_part[1:]
|
||||||
|
integer_part = "{:,}".format(int(integer_part)).replace(',', " ")
|
||||||
|
integer_part = sign + integer_part
|
||||||
|
fract_part = " ".join(fract_part[i:i+3] for i in range(0, len(fract_part), 3))
|
||||||
result = integer_part + DECIMAL_POINT + fract_part
|
result = integer_part + DECIMAL_POINT + fract_part
|
||||||
# add leading/trailing whitespaces so that numbers can be aligned in a column
|
# add leading/trailing whitespaces so that numbers can be aligned in a column
|
||||||
if whitespaces:
|
if whitespaces:
|
||||||
|
|||||||
Reference in New Issue
Block a user