Skip to content

Commit

Permalink
Change implementation of to_currency() in es_ES (#167)
Browse files Browse the repository at this point in the history
* Change implementation of to_currency()
* Change OLD to ESP and add USD
  • Loading branch information
Abraham Anes authored and erozqba committed Apr 18, 2018
1 parent 551a980 commit b492530
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 24 deletions.
17 changes: 11 additions & 6 deletions num2words/lang_ES.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@


class Num2Word_ES(Num2Word_EU):
CURRENCY_FORMS = {
'EUR': (('euro', 'euros'), ('centimo', 'centimos')),
'ESP': (('peseta', 'pesetas'), ('centimo', 'centimos')),
'USD': (('dolar', 'dolares'), ('centavo', 'centavos')),
}

# //CHECK: Is this sufficient??
def set_high_numwords(self, high):
max = 3 + 6 * len(high)
Expand Down Expand Up @@ -165,11 +171,10 @@ def to_ordinal_num(self, value):
self.verify_ordinal(value)
return "%s%s" % (value, "º" if self.gender_stem == 'o' else "ª")

def to_currency(self, val, longval=True, old=False):
hightxt, lowtxt = "euro/s", "centavo/s"
if old:
hightxt, lowtxt = "peso/s", "peseta/s"
result = self.to_splitnum(val, hightxt=hightxt, lowtxt=lowtxt,
divisor=1, jointxt="y", longval=longval)
def to_currency(self, val, currency='EUR', cents=True, seperator=' con',
adjective=False):
result = super(Num2Word_ES, self).to_currency(
val, currency=currency, cents=cents, seperator=seperator,
adjective=adjective)
# Handle exception, in spanish is "un euro" and not "uno euro"
return result.replace("uno", "un")
56 changes: 38 additions & 18 deletions tests/test_es.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,23 +110,36 @@
)

TEST_CASES_TO_CURRENCY = (
(1, 'un euro'),
(2, 'dos euros'),
(8, 'ocho euros'),
(12, 'doce euros'),
(21, 'veintiun euros'),
(81.25, 'ochenta y un euros y veinticinco centavos'),
(100, 'cien euros'),
(1.00, 'un euro con cero centimos'),
(2.00, 'dos euros con cero centimos'),
(8.00, 'ocho euros con cero centimos'),
(12.00, 'doce euros con cero centimos'),
(21.00, 'veintiun euros con cero centimos'),
(81.25, 'ochenta y un euros con veinticinco centimos'),
(350.90, 'trescientos cincuenta euros con noventa centimos'),
(100.00, 'cien euros con cero centimos'),
)

TEST_CASES_TO_CURRENCY_OLD = (
(1, 'un peso'),
(2, 'dos pesos'),
(8, 'ocho pesos'),
(12, 'doce pesos'),
(21, 'veintiun pesos'),
(81.25, 'ochenta y un pesos y veinticinco pesetas'),
(100, 'cien pesos'),
TEST_CASES_TO_CURRENCY_ESP = (
(1.00, 'un peseta con cero centimos'),
(2.00, 'dos pesetas con cero centimos'),
(8.00, 'ocho pesetas con cero centimos'),
(12.00, 'doce pesetas con cero centimos'),
(21.00, 'veintiun pesetas con cero centimos'),
(81.25, 'ochenta y un pesetas con veinticinco centimos'),
(350.90, 'trescientos cincuenta pesetas con noventa centimos'),
(100.00, 'cien pesetas con cero centimos'),
)

TEST_CASES_TO_CURRENCY_USD = (
(1.00, 'un dolar con cero centavos'),
(2.00, 'dos dolares con cero centavos'),
(8.00, 'ocho dolares con cero centavos'),
(12.00, 'doce dolares con cero centavos'),
(21.00, 'veintiun dolares con cero centavos'),
(81.25, 'ochenta y un dolares con veinticinco centavos'),
(350.90, 'trescientos cincuenta dolares con noventa centavos'),
(100.00, 'cien dolares con cero centavos'),
)


Expand Down Expand Up @@ -157,9 +170,16 @@ def test_currency(self):
test[1]
)

def test_currency_old(self):
for test in TEST_CASES_TO_CURRENCY_OLD:
def test_currency_esp(self):
for test in TEST_CASES_TO_CURRENCY_ESP:
self.assertEqual(
num2words(test[0], lang='es', to='currency', currency='ESP'),
test[1]
)

def test_currency_usd(self):
for test in TEST_CASES_TO_CURRENCY_USD:
self.assertEqual(
num2words(test[0], lang='es', to='currency', old=True),
num2words(test[0], lang='es', to='currency', currency='USD'),
test[1]
)

0 comments on commit b492530

Please sign in to comment.