From 6a431aab8cf55dec7b7807582a8bb6449f488e89 Mon Sep 17 00:00:00 2001 From: djboi <73574557+dhruv-joshi-7@users.noreply.github.com> Date: Wed, 28 Jul 2021 18:56:30 +0530 Subject: [PATCH] Fixed issue with thousands separator for better readability (#7427) util.format_satoshis: introduce new option to add thousands separators --- electrum/simple_config.py | 3 ++- electrum/tests/test_util.py | 8 ++++++++ electrum/util.py | 9 +++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/electrum/simple_config.py b/electrum/simple_config.py index 0b87dfbdf..862f9d946 100644 --- a/electrum/simple_config.py +++ b/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): diff --git a/electrum/tests/test_util.py b/electrum/tests/test_util.py index 196249613..ccabbb00f 100644 --- a/electrum/tests/test_util.py +++ b/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)) diff --git a/electrum/util.py b/electrum/util.py index 37847164c..a2a19b13a 100644 --- a/electrum/util.py +++ b/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: