Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zero cents bug (fix for test cases in original PR) #610

Merged
merged 7 commits into from
Jan 14, 2025
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
Loading