Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions num2words/lang_CA.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
23 changes: 14 additions & 9 deletions num2words/lang_ES.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions tests/test_ca.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
26 changes: 26 additions & 0 deletions tests/test_es.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down