Skip to content

Commit

Permalink
Zero cents bug (fix for test cases in original PR) (#610)
Browse files Browse the repository at this point in the history
* Update base.py to remove cents from displaying when whole number inputted

* Update test_en.py to align with updated base.py

* Update test_en_ng.py to align with updated base.py

* Update test_hu.py to align with updated base.py

* Update test_nl.py to align with updated base.py

* Update test_am.py to align with updated base.py

* Remove Amharic cardinals test cases meant for other PR (#598) and fix
formatting

---------

Co-authored-by: cheng-jess <[email protected]>
  • Loading branch information
bryananderson and cheng-jess authored Jan 14, 2025
1 parent 5267ce1 commit 64492d2
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 17 deletions.
37 changes: 26 additions & 11 deletions num2words/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
2 changes: 1 addition & 1 deletion tests/test_am.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_en.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_en_ng.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_to_currency(self):
separator=separator,
kobo=False
),
"zero naira and 00 kobo"
"zero naira"
)

self.assertEqual(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_hu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions tests/test_nl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down

0 comments on commit 64492d2

Please sign in to comment.