diff options
author | Florian Dold <florian.dold@gmail.com> | 2019-12-18 21:23:13 +0100 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2019-12-18 21:24:33 +0100 |
commit | 7641c1a66caf87858255320d88673223f0dd1f31 (patch) | |
tree | cdbfba265a670cf6b7926f149b12b4c07db8ac67 | |
parent | c3a74e694cd57179460294244f060dd6892d806d (diff) | |
download | taler-util-7641c1a66caf87858255320d88673223f0dd1f31.tar.gz taler-util-7641c1a66caf87858255320d88673223f0dd1f31.tar.bz2 taler-util-7641c1a66caf87858255320d88673223f0dd1f31.zip |
fix infinite loop in stringify and add test case
Also bump to 0.6.2
-rw-r--r-- | setup.py | 2 | ||||
-rw-r--r-- | taler/util/amount.py | 7 | ||||
-rwxr-xr-x | tests/test_amount.py | 1 |
3 files changed, 6 insertions, 4 deletions
@@ -25,7 +25,7 @@ with open('README', 'r') as f: setup( name='taler-util', - version='0.6.1', + version='0.6.2', license='LGPL3+', platforms='any', author='Taler Systems SA', diff --git a/taler/util/amount.py b/taler/util/amount.py index c768f30..3891326 100644 --- a/taler/util/amount.py +++ b/taler/util/amount.py @@ -232,7 +232,7 @@ class Amount: # Convert the amount to a string. # # @param self this object. - # @param ndigits how many digits we want for the fractional part. + # @param ndigits minimum number of digits to display in the fractional part # @param pretty if True, put the currency in the last position and # omit the colon. def stringify(self, ndigits=0, pretty=False) -> str: @@ -240,10 +240,11 @@ class Amount: if self.fraction != 0: s += "." frac = self.fraction - while frac != 0 or ndigits != 0: + while frac > 0 or (ndigits is not None and ndigits > 0): s += str(int(frac / (Amount._fraction() / 10))) frac = (frac * 10) % (Amount._fraction()) - ndigits -= 1 + if ndigits > 0: + ndigits -= 1 elif ndigits != 0: s += "." + ("0" * ndigits) if not pretty: diff --git a/tests/test_amount.py b/tests/test_amount.py index 67d9844..a4043b7 100755 --- a/tests/test_amount.py +++ b/tests/test_amount.py @@ -68,3 +68,4 @@ class TestAmount(TestCase): self.assertEqual(self.amount.stringify(3), 'TESTKUDOS:0.000') self.amount.add(Amount('TESTKUDOS', 2, 100)) self.assertEqual(self.amount.stringify(6), 'TESTKUDOS:2.000001') + self.assertEqual(Amount("TESTKUDOS", value=5, fraction=9000000).stringify(), 'TESTKUDOS:5.09') |