Change implementation of to_currency() in es_ES (#167)

* Change implementation of to_currency()
* Change OLD to ESP and add USD
This commit is contained in:
Abraham Anes
2018-04-18 15:05:13 +02:00
committed by Ernesto Rodriguez Ortiz
parent 551a980e54
commit b492530cfa
2 changed files with 49 additions and 24 deletions

View File

@@ -22,6 +22,12 @@ from .lang_EU import Num2Word_EU
class Num2Word_ES(Num2Word_EU): 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?? # //CHECK: Is this sufficient??
def set_high_numwords(self, high): def set_high_numwords(self, high):
max = 3 + 6 * len(high) max = 3 + 6 * len(high)
@@ -165,11 +171,10 @@ class Num2Word_ES(Num2Word_EU):
self.verify_ordinal(value) self.verify_ordinal(value)
return "%s%s" % (value, "º" if self.gender_stem == 'o' else "ª") return "%s%s" % (value, "º" if self.gender_stem == 'o' else "ª")
def to_currency(self, val, longval=True, old=False): def to_currency(self, val, currency='EUR', cents=True, seperator=' con',
hightxt, lowtxt = "euro/s", "centavo/s" adjective=False):
if old: result = super(Num2Word_ES, self).to_currency(
hightxt, lowtxt = "peso/s", "peseta/s" val, currency=currency, cents=cents, seperator=seperator,
result = self.to_splitnum(val, hightxt=hightxt, lowtxt=lowtxt, adjective=adjective)
divisor=1, jointxt="y", longval=longval)
# Handle exception, in spanish is "un euro" and not "uno euro" # Handle exception, in spanish is "un euro" and not "uno euro"
return result.replace("uno", "un") return result.replace("uno", "un")

View File

@@ -110,23 +110,36 @@ TEST_CASES_ORDINAL_NUM = (
) )
TEST_CASES_TO_CURRENCY = ( TEST_CASES_TO_CURRENCY = (
(1, 'un euro'), (1.00, 'un euro con cero centimos'),
(2, 'dos euros'), (2.00, 'dos euros con cero centimos'),
(8, 'ocho euros'), (8.00, 'ocho euros con cero centimos'),
(12, 'doce euros'), (12.00, 'doce euros con cero centimos'),
(21, 'veintiun euros'), (21.00, 'veintiun euros con cero centimos'),
(81.25, 'ochenta y un euros y veinticinco centavos'), (81.25, 'ochenta y un euros con veinticinco centimos'),
(100, 'cien euros'), (350.90, 'trescientos cincuenta euros con noventa centimos'),
(100.00, 'cien euros con cero centimos'),
) )
TEST_CASES_TO_CURRENCY_OLD = ( TEST_CASES_TO_CURRENCY_ESP = (
(1, 'un peso'), (1.00, 'un peseta con cero centimos'),
(2, 'dos pesos'), (2.00, 'dos pesetas con cero centimos'),
(8, 'ocho pesos'), (8.00, 'ocho pesetas con cero centimos'),
(12, 'doce pesos'), (12.00, 'doce pesetas con cero centimos'),
(21, 'veintiun pesos'), (21.00, 'veintiun pesetas con cero centimos'),
(81.25, 'ochenta y un pesos y veinticinco pesetas'), (81.25, 'ochenta y un pesetas con veinticinco centimos'),
(100, 'cien pesos'), (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'),
) )
@@ -157,9 +170,16 @@ class Num2WordsESTest(TestCase):
test[1] test[1]
) )
def test_currency_old(self): def test_currency_esp(self):
for test in TEST_CASES_TO_CURRENCY_OLD: for test in TEST_CASES_TO_CURRENCY_ESP:
self.assertEqual( self.assertEqual(
num2words(test[0], lang='es', to='currency', old=True), 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', currency='USD'),
test[1] test[1]
) )