diff --git a/num2words/base.py b/num2words/base.py index 243045ff..c0c2481e 100644 --- a/num2words/base.py +++ b/num2words/base.py @@ -278,6 +278,7 @@ def to_currency(self, val, currency='EUR', cents=True, separator=',', Returns: str: Formatted string + Handles whole numbers and decimal numbers differently """ left, right, is_negative = parse_currency_parts(val) @@ -294,17 +295,31 @@ def to_currency(self, val, currency='EUR', cents=True, separator=',', minus_str = "%s " % self.negword.strip() if is_negative else "" money_str = self._money_verbose(left, currency) - cents_str = self._cents_verbose(right, currency) \ - if cents else self._cents_terse(right, currency) - - return u'%s%s %s%s %s %s' % ( - minus_str, - money_str, - self.pluralize(left, cr1), - separator, - cents_str, - self.pluralize(right, cr2) - ) + + # Explicitly check if input has decimal point or non-zero cents + has_decimal = isinstance(val, float) or str(val).find('.') != -1 + + # Only include cents if: + # 1. Input has decimal point OR + # 2. Cents are non-zero + if has_decimal or right > 0: + cents_str = self._cents_verbose(right, currency) \ + if cents else self._cents_terse(right, currency) + + return u'%s%s %s%s %s %s' % ( + minus_str, + money_str, + self.pluralize(left, cr1), + separator, + cents_str, + self.pluralize(right, cr2) + ) + else: + return u'%s%s %s' % ( + minus_str, + money_str, + self.pluralize(left, cr1) + ) def setup(self): pass diff --git a/tests/test_am.py b/tests/test_am.py index 58709c37..2712f6e7 100644 --- a/tests/test_am.py +++ b/tests/test_am.py @@ -73,7 +73,7 @@ def test_to_currency(self): ) self.assertEqual( num2words('0', lang='am', to='currency', separator=' እና', - cents=True, currency='ETB'), 'ዜሮ ብር እና ዜሮ ሳንቲም' + cents=True, currency='ETB'), 'ዜሮ ብር' ) self.assertEqual( diff --git a/tests/test_en.py b/tests/test_en.py index 4ab267c6..48abc588 100644 --- a/tests/test_en.py +++ b/tests/test_en.py @@ -86,7 +86,7 @@ def test_to_currency(self): self.assertEqual( num2words('0', lang='en', to='currency', separator=' and', cents=False, currency='USD'), - "zero dollars and 00 cents" + "zero dollars" ) self.assertEqual( diff --git a/tests/test_en_ng.py b/tests/test_en_ng.py index 6d7e72e5..6e5343b7 100644 --- a/tests/test_en_ng.py +++ b/tests/test_en_ng.py @@ -51,7 +51,7 @@ def test_to_currency(self): separator=separator, kobo=False ), - "zero naira and 00 kobo" + "zero naira" ) self.assertEqual( diff --git a/tests/test_hu.py b/tests/test_hu.py index d0c159dc..5041e03e 100644 --- a/tests/test_hu.py +++ b/tests/test_hu.py @@ -169,7 +169,7 @@ def test_to_currency(self): self.assertEqual( num2words('0', lang='hu', to='currency', separator=' és', cents=False, currency='HUF'), - "nulla forint és 00 fillér" + "nulla forint" ) self.assertEqual( diff --git a/tests/test_nl.py b/tests/test_nl.py index 95c690f4..56af50f7 100644 --- a/tests/test_nl.py +++ b/tests/test_nl.py @@ -77,7 +77,7 @@ def test_to_currency_eur(self): self.assertEqual( num2words('0', lang='nl', to='currency', separator=' en', cents=False, currency='EUR'), - "nul euro en 00 cent" + "nul euro" ) self.assertEqual( @@ -100,7 +100,7 @@ def test_to_currency_usd(self): self.assertEqual( num2words('0', lang='nl', to='currency', separator=' en', cents=False, currency='USD'), - "nul dollar en 00 cent" + "nul dollar" ) self.assertEqual(