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

Fixed Currency to only Display Cents When Needed #602

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
39 changes: 27 additions & 12 deletions num2words/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def _cents_terse(self, number, currency):
return "%02d" % number

def to_currency(self, val, currency='EUR', cents=True, separator=',',
adjective=False):
adjective=False):
"""
Args:
val: Numeric value
Expand All @@ -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
8 changes: 7 additions & 1 deletion tests/test_am.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ def test_cardinal(self):
self.assertEqual(num2words(100, lang='am'), 'መቶ')
self.assertEqual(num2words(100000, lang='am'), 'አንድ መቶ ሺህ')
self.assertEqual(num2words(101, lang='am'), 'አንድ መቶ አንድ')
self.assertEqual(num2words(568476685, lang='am'), 'አምስት መቶ ስድሳ ስምንት mሚሊዮን, አራት መቶ ሰባ ስድስት ሺህ, ስድስት መቶ ሰማኒያ አምስት')
self.assertEqual(num2words(56847, lang='am'), 'አምሳ ስድስት ሺህ, ስምንት መቶ አርባ ሰባት')
self.assertEqual(num2words(1111111111111111111, lang='am'), 'አንድ quintሚሊዮን, አንድ መቶ አሥራ አንድ quadrሚሊዮን, አንድ መቶ አሥራ አንድ trሚሊዮን, አንድ መቶ አሥራ አንድ bቢሊዮን, አንድ መቶ አሥራ አንድ mሚሊዮን, አንድ መቶ አሥራ አንድ ሺህ, አንድ መቶ አሥራ አንድ')
self.assertEqual(num2words(999999999, lang='am'), 'ዘጠኝ መቶ ዘጠና ዘጠኝ mሚሊዮን, ዘጠኝ መቶ ዘጠና ዘጠኝ ሺህ, ዘጠኝ መቶ ዘጠና ዘጠኝ')
self.assertEqual(num2words(29498237468376240, lang="am"), 'ሃያ ዘጠኝ quadrሚሊዮን, አራት መቶ ዘጠና ስምንት trሚሊዮን, ሁለት መቶ ሠላሳ ሰባት bቢሊዮን, አራት መቶ ስድሳ ስምንት mሚሊዮን, ሦስት መቶ ሰባ ስድስት ሺህ, ሁለት መቶ አርባ')
self.assertEqual(num2words(110110, lang='am'), 'አንድ መቶ አሥር ሺህ, አንድ መቶ አሥር')

def test_and_join_199(self):
self.assertEqual(num2words(199, lang='am'), 'አንድ መቶ ዘጠና ዘጠኝ')
Expand Down Expand Up @@ -73,7 +79,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