diff --git a/electrum/tests/test_util.py b/electrum/tests/test_util.py index ccabbb00f..cf204bb61 100644 --- a/electrum/tests/test_util.py +++ b/electrum/tests/test_util.py @@ -74,10 +74,21 @@ class TestUtil(ElectrumTestCase): 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)) + # is_diff 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)) + # num_zeros + self.assertEqual("-456 789.123 400", format_satoshis(-45678912340000, num_zeros=6, add_thousands_sep=True)) + self.assertEqual("-456 789.123 4", format_satoshis(-45678912340000, num_zeros=2, add_thousands_sep=True)) + # whitespaces + self.assertEqual(" 1 432.731 11", format_satoshis(143273111, decimal_point=5, add_thousands_sep=True, whitespaces=True)) + self.assertEqual(" 1 432.731 ", format_satoshis(143273100, decimal_point=5, add_thousands_sep=True, whitespaces=True)) + self.assertEqual(" 67 891 432.731 ", format_satoshis(6789143273100, decimal_point=5, add_thousands_sep=True, whitespaces=True)) + self.assertEqual(" 143 273 100.", format_satoshis(143273100, decimal_point=0, add_thousands_sep=True, whitespaces=True)) + self.assertEqual(" 6 789 143 273 100.", format_satoshis(6789143273100, decimal_point=0, add_thousands_sep=True, whitespaces=True)) + self.assertEqual("56 789 143 273 100.", format_satoshis(56789143273100, decimal_point=0, add_thousands_sep=True, whitespaces=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 a2a19b13a..bcb275165 100644 --- a/electrum/util.py +++ b/electrum/util.py @@ -693,11 +693,16 @@ def format_satoshis( result = integer_part + DECIMAL_POINT + fract_part # add leading/trailing whitespaces so that numbers can be aligned in a column if whitespaces: + target_fract_len = overall_precision + target_integer_len = 14 - decimal_point # should be enough for up to unsigned 999999 BTC + if add_thousands_sep: + target_fract_len += max(0, (target_fract_len - 1) // 3) + target_integer_len += max(0, (target_integer_len - 1) // 3) # add trailing whitespaces - result += " " * (overall_precision - len(fract_part)) + result += " " * (target_fract_len - len(fract_part)) # add leading whitespaces - target_len = 15 + precision - result = " " * (target_len - len(result)) + result + target_total_len = target_integer_len + 1 + target_fract_len + result = " " * (target_total_len - len(result)) + result return result