Browse Source

Fixed issue with thousands separator for better readability (#7427)

util.format_satoshis: introduce new option to add thousands separators
patch-4
djboi 4 years ago
committed by GitHub
parent
commit
6a431aab8c
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      electrum/simple_config.py
  2. 8
      electrum/tests/test_util.py
  3. 9
      electrum/util.py

3
electrum/simple_config.py

@ -659,7 +659,7 @@ class SimpleConfig(Logger):
except:
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(
x,
num_zeros=self.num_zeros,
@ -667,6 +667,7 @@ class SimpleConfig(Logger):
is_diff=is_diff,
whitespaces=whitespaces,
precision=self.amt_precision_post_satoshi,
add_thousands_sep=add_thousands_sep,
)
def format_amount_and_units(self, amount):

8
electrum/tests/test_util.py

@ -70,6 +70,14 @@ class TestUtil(ElectrumTestCase):
def test_format_satoshis_diff_negative(self):
self.assertEqual("-0.00001234", format_satoshis(-1234, 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):
self.assertEqual("0.00001234", format_satoshis_plain(1234))

9
electrum/util.py

@ -659,6 +659,7 @@ def format_satoshis(
precision: int = 0, # extra digits after satoshi precision
is_diff: bool = False, # if True, enforce a leading sign (+/-)
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:
if x is None:
return 'unknown'
@ -681,6 +682,14 @@ def format_satoshis(
integer_part, fract_part = result.split(".")
if len(fract_part) < num_zeros:
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
# add leading/trailing whitespaces so that numbers can be aligned in a column
if whitespaces:

Loading…
Cancel
Save