Browse Source

util.format_satoshis: fix whitespaces param for non-zero precision

patch-4
SomberNight 4 years ago
parent
commit
46badd128e
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 16
      electrum/tests/test_util.py
  2. 9
      electrum/util.py

16
electrum/tests/test_util.py

@ -52,16 +52,16 @@ class TestUtil(ElectrumTestCase):
format_fee_satoshis(1666/1000, precision=1))
def test_format_satoshis_whitespaces(self):
self.assertEqual(" 0.0001234 ",
format_satoshis(12340, whitespaces=True))
self.assertEqual(" 0.00001234",
format_satoshis(1234, whitespaces=True))
self.assertEqual(" 0.0001234 ", format_satoshis(12340, whitespaces=True))
self.assertEqual(" 0.00001234", format_satoshis(1234, whitespaces=True))
self.assertEqual(" 0.45831275", format_satoshis(Decimal("45831275."), whitespaces=True))
self.assertEqual(" 0.45831275 ", format_satoshis(Decimal("45831275."), whitespaces=True, precision=3))
self.assertEqual(" 0.458312757 ", format_satoshis(Decimal("45831275.7"), whitespaces=True, precision=3))
self.assertEqual(" 0.45831275748", format_satoshis(Decimal("45831275.748"), whitespaces=True, precision=3))
def test_format_satoshis_whitespaces_negative(self):
self.assertEqual(" -0.0001234 ",
format_satoshis(-12340, whitespaces=True))
self.assertEqual(" -0.00001234",
format_satoshis(-1234, whitespaces=True))
self.assertEqual(" -0.0001234 ", format_satoshis(-12340, whitespaces=True))
self.assertEqual(" -0.00001234", format_satoshis(-1234, whitespaces=True))
def test_format_satoshis_diff_positive(self):
self.assertEqual("+0.00001234", format_satoshis(1234, is_diff=True))

9
electrum/util.py

@ -677,10 +677,13 @@ def format_satoshis(
if len(fract_part) < num_zeros:
fract_part += "0" * (num_zeros - len(fract_part))
result = integer_part + DECIMAL_POINT + fract_part
# add leading/trailing whitespaces
# add leading/trailing whitespaces so that numbers can be aligned in a column
if whitespaces:
result += " " * (decimal_point - len(fract_part))
result = " " * (15 - len(result)) + result
# add trailing whitespaces
result += " " * (overall_precision - len(fract_part))
# add leading whitespaces
target_len = 15 + precision
result = " " * (target_len - len(result)) + result
return result

Loading…
Cancel
Save