From 9623dbcce1418f420906129f27c841af02b4ae96 Mon Sep 17 00:00:00 2001 From: cheng-jess <143574087+cheng-jess@users.noreply.github.com> Date: Wed, 4 Dec 2024 15:21:04 -0500 Subject: [PATCH 1/7] Update base.py to remove cents from displaying when whole number inputted --- num2words/base.py | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/num2words/base.py b/num2words/base.py index 243045ff..8804b884 100644 --- a/num2words/base.py +++ b/num2words/base.py @@ -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 @@ -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 From 058497114f309e4e6bf2a95dae5803d9988dff83 Mon Sep 17 00:00:00 2001 From: cheng-jess <143574087+cheng-jess@users.noreply.github.com> Date: Wed, 4 Dec 2024 15:21:42 -0500 Subject: [PATCH 2/7] Update test_en.py to align with updated base.py --- tests/test_en.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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( From 05f00cea68250e54c2962aad76fad2b56543cd09 Mon Sep 17 00:00:00 2001 From: cheng-jess <143574087+cheng-jess@users.noreply.github.com> Date: Wed, 4 Dec 2024 15:22:03 -0500 Subject: [PATCH 3/7] Update test_en_ng.py to align with updated base.py --- tests/test_en_ng.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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( From d2226b94df7e0528b15c30b4a1918ce118e9c49e Mon Sep 17 00:00:00 2001 From: cheng-jess <143574087+cheng-jess@users.noreply.github.com> Date: Wed, 4 Dec 2024 15:22:25 -0500 Subject: [PATCH 4/7] Update test_hu.py to align with updated base.py --- tests/test_hu.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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( From bbe8c8ba9715f5ae3afc79bdc219a1c19674a982 Mon Sep 17 00:00:00 2001 From: cheng-jess <143574087+cheng-jess@users.noreply.github.com> Date: Wed, 4 Dec 2024 15:22:57 -0500 Subject: [PATCH 5/7] Update test_nl.py to align with updated base.py --- tests/test_nl.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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( From 9d1ed2bfeae394de111a5e0d2b2330c7c4cd7f20 Mon Sep 17 00:00:00 2001 From: cheng-jess <143574087+cheng-jess@users.noreply.github.com> Date: Wed, 4 Dec 2024 15:23:21 -0500 Subject: [PATCH 6/7] Update test_am.py to align with updated base.py --- tests/test_am.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/test_am.py b/tests/test_am.py index 58709c37..e1d40852 100644 --- a/tests/test_am.py +++ b/tests/test_am.py @@ -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'), 'አንድ መቶ ዘጠና ዘጠኝ') @@ -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( From 127a577099675729a8d4e530e88d1444c48a01e6 Mon Sep 17 00:00:00 2001 From: Bryan Anderson Date: Wed, 8 Jan 2025 16:20:24 -0800 Subject: [PATCH 7/7] Remove Amharic cardinals test cases meant for other PR (#598) and fix formatting --- num2words/base.py | 4 ++-- tests/test_am.py | 6 ------ 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/num2words/base.py b/num2words/base.py index 8804b884..c0c2481e 100644 --- a/num2words/base.py +++ b/num2words/base.py @@ -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 @@ -305,7 +305,7 @@ def to_currency(self, val, currency='EUR', cents=True, separator=',', 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, diff --git a/tests/test_am.py b/tests/test_am.py index e1d40852..2712f6e7 100644 --- a/tests/test_am.py +++ b/tests/test_am.py @@ -25,12 +25,6 @@ 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'), 'አንድ መቶ ዘጠና ዘጠኝ')