1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-13 19:18:56 +00:00

core: fix utils.format_amount for negative amounts

This commit is contained in:
Pavol Rusnak 2019-08-23 12:24:17 +02:00
parent 2c709ee162
commit e1f0c238cf
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
2 changed files with 16 additions and 1 deletions

View File

@ -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

View File

@ -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()