Skip to content

Commit 64492d2

Browse files
Zero cents bug (fix for test cases in original PR) (#610)
* 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]>
1 parent 5267ce1 commit 64492d2

File tree

6 files changed

+32
-17
lines changed

6 files changed

+32
-17
lines changed

num2words/base.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ def to_currency(self, val, currency='EUR', cents=True, separator=',',
278278
Returns:
279279
str: Formatted string
280280
281+
Handles whole numbers and decimal numbers differently
281282
"""
282283
left, right, is_negative = parse_currency_parts(val)
283284

@@ -294,17 +295,31 @@ def to_currency(self, val, currency='EUR', cents=True, separator=',',
294295

295296
minus_str = "%s " % self.negword.strip() if is_negative else ""
296297
money_str = self._money_verbose(left, currency)
297-
cents_str = self._cents_verbose(right, currency) \
298-
if cents else self._cents_terse(right, currency)
299-
300-
return u'%s%s %s%s %s %s' % (
301-
minus_str,
302-
money_str,
303-
self.pluralize(left, cr1),
304-
separator,
305-
cents_str,
306-
self.pluralize(right, cr2)
307-
)
298+
299+
# Explicitly check if input has decimal point or non-zero cents
300+
has_decimal = isinstance(val, float) or str(val).find('.') != -1
301+
302+
# Only include cents if:
303+
# 1. Input has decimal point OR
304+
# 2. Cents are non-zero
305+
if has_decimal or right > 0:
306+
cents_str = self._cents_verbose(right, currency) \
307+
if cents else self._cents_terse(right, currency)
308+
309+
return u'%s%s %s%s %s %s' % (
310+
minus_str,
311+
money_str,
312+
self.pluralize(left, cr1),
313+
separator,
314+
cents_str,
315+
self.pluralize(right, cr2)
316+
)
317+
else:
318+
return u'%s%s %s' % (
319+
minus_str,
320+
money_str,
321+
self.pluralize(left, cr1)
322+
)
308323

309324
def setup(self):
310325
pass

tests/test_am.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def test_to_currency(self):
7373
)
7474
self.assertEqual(
7575
num2words('0', lang='am', to='currency', separator=' እና',
76-
cents=True, currency='ETB'), 'ዜሮ ብር እና ዜሮ ሳንቲም'
76+
cents=True, currency='ETB'), 'ዜሮ ብር'
7777
)
7878

7979
self.assertEqual(

tests/test_en.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def test_to_currency(self):
8686
self.assertEqual(
8787
num2words('0', lang='en', to='currency', separator=' and',
8888
cents=False, currency='USD'),
89-
"zero dollars and 00 cents"
89+
"zero dollars"
9090
)
9191

9292
self.assertEqual(

tests/test_en_ng.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def test_to_currency(self):
5151
separator=separator,
5252
kobo=False
5353
),
54-
"zero naira and 00 kobo"
54+
"zero naira"
5555
)
5656

5757
self.assertEqual(

tests/test_hu.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def test_to_currency(self):
169169
self.assertEqual(
170170
num2words('0', lang='hu', to='currency', separator=' és',
171171
cents=False, currency='HUF'),
172-
"nulla forint és 00 fillér"
172+
"nulla forint"
173173
)
174174

175175
self.assertEqual(

tests/test_nl.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def test_to_currency_eur(self):
7777
self.assertEqual(
7878
num2words('0', lang='nl', to='currency', separator=' en',
7979
cents=False, currency='EUR'),
80-
"nul euro en 00 cent"
80+
"nul euro"
8181
)
8282

8383
self.assertEqual(
@@ -100,7 +100,7 @@ def test_to_currency_usd(self):
100100
self.assertEqual(
101101
num2words('0', lang='nl', to='currency', separator=' en',
102102
cents=False, currency='USD'),
103-
"nul dollar en 00 cent"
103+
"nul dollar"
104104
)
105105

106106
self.assertEqual(

0 commit comments

Comments
 (0)