core: fix utils.format_amount for negative amounts

pull/114/merge
Pavol Rusnak 5 years ago
parent 2c709ee162
commit e1f0c238cf
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D

@ -74,8 +74,13 @@ def chunks(items: List[Chunked], size: int) -> Iterator[List[Chunked]]:
def format_amount(amount: int, decimals: int) -> str:
if amount < 0:
amount = -amount
sign = "-"
else:
sign = ""
d = pow(10, decimals)
s = ("%d.%0*d" % (amount // d, decimals, amount % d)).rstrip("0").rstrip(".")
s = ("%s%d.%0*d" % (sign, amount // d, decimals, amount % d)).rstrip("0").rstrip(".")
return s

@ -13,6 +13,16 @@ class TestUtils(unittest.TestCase):
self.assertEqual(c[i].stop, 100 if (i == 14) else (i + 1) * 7)
self.assertEqual(c[i].step, 1)
def test_format_amount(self):
VECTORS = [
(123456, 3, "123.456"),
(4242, 7, "0.0004242"),
(-123456, 3, "-123.456"),
(-4242, 7, "-0.0004242"),
]
for v in VECTORS:
self.assertEqual(utils.format_amount(v[0], v[1]), v[2])
if __name__ == '__main__':
unittest.main()

Loading…
Cancel
Save