diff --git a/num2words/lang_CA.py b/num2words/lang_CA.py index 97d0f29e..a386ac73 100644 --- a/num2words/lang_CA.py +++ b/num2words/lang_CA.py @@ -463,13 +463,16 @@ def to_currency(self, val, currency="EUR", cents=True, list_result[0] = list_result[0].replace(" i un", "-un") list_result[0] = list_result[0].replace("un", "un") - if currency in CENTS_UNA: - list_result[1] = list_result[1].replace("un", "una") - list_result[1] = list_result[1].replace("dos", "dues") + # The base to_currency() omits the cents part for whole amounts + # (e.g. integer inputs with no cents), so it may be absent here. + if len(list_result) > 1: + if currency in CENTS_UNA: + list_result[1] = list_result[1].replace("un", "una") + list_result[1] = list_result[1].replace("dos", "dues") - list_result[1] = list_result[1].replace("vint-i-un", "vint-i-una") + list_result[1] = list_result[1].replace("vint-i-un", "vint-i-una") - list_result[1] = list_result[1].replace("un", "un") + list_result[1] = list_result[1].replace("un", "un") result = (separator + " ").join(list_result) diff --git a/num2words/lang_ES.py b/num2words/lang_ES.py index ce1c9f8b..915fef58 100644 --- a/num2words/lang_ES.py +++ b/num2words/lang_ES.py @@ -397,18 +397,23 @@ def to_currency(self, val, currency='EUR', cents=True, separator=' con', # "CENTS" PART (list_result[1]) - # Feminine "cents" ("una piastra", "veintiuna piastras"...) - if currency in CENTS_UNA: + # The base to_currency() omits the cents part for whole amounts + # (e.g. integer inputs with no cents), so it may be absent here. + if len(list_result) > 1: - # "una piastra", "veintiuna piastras", "treinta y una piastras"... - list_result[1] = list_result[1].replace("uno", "una") + # Feminine "cents" ("una piastra", "veintiuna piastras"...) + if currency in CENTS_UNA: - # Masc.: Correct orthography for the specific case of "veintiún": - list_result[1] = list_result[1].replace("veintiuno", "veintiún") + # "una piastra", "veintiuna piastras", "treinta y una + # piastras"... + list_result[1] = list_result[1].replace("uno", "una") + + # Masc.: Correct orthography for the specific case of "veintiún": + list_result[1] = list_result[1].replace("veintiuno", "veintiún") - # Masculine "cents": general case ("un centavo", "treinta y un - # centavos"...): - list_result[1] = list_result[1].replace("uno", "un") + # Masculine "cents": general case ("un centavo", "treinta y un + # centavos"...): + list_result[1] = list_result[1].replace("uno", "un") # join back "dollars" part with "cents" part result = (separator + " ").join(list_result) diff --git a/tests/test_ca.py b/tests/test_ca.py index ab5be158..4ac8cb99 100644 --- a/tests/test_ca.py +++ b/tests/test_ca.py @@ -173,6 +173,20 @@ def test_ordinal_num(self): def test_currency(self): self._test_cases(TEST_CASES_TO_CURRENCY, to="currency", currency="EUR") + def test_currency_integer_whole_units(self): + # Regression: integer inputs are whole currency units with no cents, + # so the base to_currency() omits the cents segment. The CA override + # used to assume that segment always existed and raised IndexError. + cases = ( + (0, 'zero euros'), + (100, 'un euro'), + (200, 'dos euros'), + ) + for value, expected in cases: + self.assertEqual( + num2words(value, lang='ca', to='currency'), expected + ) + def test_currency_esp(self): self._test_cases(TEST_CASES_TO_CURRENCY_ESP, to="currency", currency="ESP") diff --git a/tests/test_es.py b/tests/test_es.py index ce2bb033..4005ff93 100644 --- a/tests/test_es.py +++ b/tests/test_es.py @@ -1866,6 +1866,32 @@ def test_currency(self): test[1] ) + def test_currency_integer_whole_units(self): + # Regression: integer inputs resolve to whole currency units with no + # cents, so the base to_currency() omits the cents segment. The ES + # override used to assume that segment always existed and raised + # IndexError. See whole-unit amounts below. + cases = ( + (0, 'cero euros'), + (100, 'un euro'), + (200, 'dos euros'), + (2100, 'veintiún euros'), + (12300, 'ciento veintitrés euros'), + ) + for value, expected in cases: + self.assertEqual( + num2words(value, lang='es', to='currency'), expected + ) + # Feminine currency keeps its grammar on the whole-unit path. + self.assertEqual( + num2words(100, lang='es', to='currency', currency='GBP'), + 'una libra' + ) + self.assertEqual( + num2words(2100, lang='es', to='currency', currency='GBP'), + 'veintiuna libras' + ) + def test_currency_esp(self): for test in TEST_CASES_TO_CURRENCY_ESP: self.assertEqual(