Fixing currency functions for german and french. Adding Currency_Forms for DE, FR and NL. (#247)

* Fix currency function for French. Adding Currency_Forms.

* Fix currency function for German. Adding Currency_Forms.

* Move FR_DZ to new currency functions, too.

* Fix tests

* Add more currency options for dutch.

* Add more tests for German, Dutch and French currencies.
This commit is contained in:
Sarah Beranek
2019-04-12 01:07:44 +02:00
committed by Ernesto Rodriguez Ortiz
parent 18194b52ef
commit a869745813
10 changed files with 211 additions and 114 deletions

View File

@@ -23,6 +23,14 @@ from .lang_EU import Num2Word_EU
class Num2Word_DE(Num2Word_EU): class Num2Word_DE(Num2Word_EU):
CURRENCY_FORMS = {
'EUR': (('Euro', 'Euro'), ('Cent', 'Cent')),
'GBP': (('Pfund', 'Pfund'), ('Penny', 'Pence')),
'USD': (('Dollar', 'Dollar'), ('Cent', 'Cent')),
'CNY': (('Yuan', 'Yuan'), ('Jiao', 'Fen')),
'DEM': (('Mark', 'Mark'), ('Pfennig', 'Pfennig')),
}
GIGA_SUFFIX = "illiarde" GIGA_SUFFIX = "illiarde"
MEGA_SUFFIX = "illion" MEGA_SUFFIX = "illion"
@@ -134,22 +142,13 @@ class Num2Word_DE(Num2Word_EU):
self.verify_ordinal(value) self.verify_ordinal(value)
return str(value) + "." return str(value) + "."
def to_currency(self, val, longval=True, old=False): def to_currency(self, val, currency='EUR', cents=True, separator=' und',
hightxt = "Euro" adjective=False):
lowtxt = "Cent" result = super(Num2Word_DE, self).to_currency(
if old: val, currency=currency, cents=cents, separator=separator,
hightxt = "Mark" adjective=adjective)
lowtxt = "Pfennig/e"
cents = int(round(val*100))
res = self.to_splitnum(cents, hightxt=hightxt, lowtxt=lowtxt,
jointxt="und", longval=longval)
# Handle exception, in german is "ein Euro" and not "eins Euro" # Handle exception, in german is "ein Euro" and not "eins Euro"
if res.startswith("eins "): return result.replace("eins ", "ein ")
res = res.replace("eins ", "ein ", 1)
return res
def to_year(self, val, longval=True): def to_year(self, val, longval=True):
if not (val // 100) % 10: if not (val // 100) % 10:

View File

@@ -21,6 +21,14 @@ from .lang_EU import Num2Word_EU
class Num2Word_FR(Num2Word_EU): class Num2Word_FR(Num2Word_EU):
CURRENCY_FORMS = {
'EUR': (('euro', 'euros'), ('centime', 'centimes')),
'USD': (('dollar', 'dollars'), ('cent', 'cents')),
'FRF': (('franc', 'francs'), ('centime', 'centimes')),
'GBP': (('livre', 'livres'), ('penny', 'pence')),
'CNY': (('yuan', 'yuans'), ('fen', 'jiaos')),
}
def setup(self): def setup(self):
Num2Word_EU.setup(self) Num2Word_EU.setup(self)
@@ -92,10 +100,9 @@ class Num2Word_FR(Num2Word_EU):
out += "er" if value == 1 else "me" out += "er" if value == 1 else "me"
return out return out
def to_currency(self, val, longval=True, old=False): def to_currency(self, val, currency='EUR', cents=True, separator=' et',
hightxt = "euro/s" adjective=False):
if old: result = super(Num2Word_FR, self).to_currency(
hightxt = "franc/s" val, currency=currency, cents=cents, separator=separator,
cents = int(round(val*100)) adjective=adjective)
return self.to_splitnum(cents, hightxt=hightxt, lowtxt="centime/s", return result
divisor=100, jointxt="et", longval=longval)

View File

@@ -21,8 +21,13 @@ from .lang_FR import Num2Word_FR
class Num2Word_FR_DZ(Num2Word_FR): class Num2Word_FR_DZ(Num2Word_FR):
def to_currency(self, val, longval=True, cents=True, jointxt="virgule"): CURRENCY_FORMS = {
return self.to_splitnum( 'DIN': (('dinard', 'dinards'), ('centime', 'centimes')),
val, hightxt="dinard/s", lowtxt="centime/s", divisor=1, }
jointxt=jointxt, longval=longval, cents=cents
) def to_currency(self, val, currency='DIN', cents=True, separator=' et',
adjective=False):
result = super(Num2Word_FR, self).to_currency(
val, currency=currency, cents=cents, separator=separator,
adjective=adjective)
return result

View File

@@ -22,7 +22,10 @@ from .lang_EU import Num2Word_EU
class Num2Word_NL(Num2Word_EU): class Num2Word_NL(Num2Word_EU):
CURRENCY_FORMS = { CURRENCY_FORMS = {
'EUR': (('euro', 'euros'), ('cent', 'cents')), 'EUR': (('euro', 'euro'), ('cent', 'cent')),
'GBP': (('pond', 'pond'), ('penny', 'pence')),
'USD': (('dollar', 'dollar'), ('cent', 'cent')),
'CNY': (('yuan', 'yuan'), ('jiao', 'fen')),
} }
GIGA_SUFFIX = "iljard" GIGA_SUFFIX = "iljard"

View File

@@ -21,6 +21,46 @@ from unittest import TestCase
from num2words import num2words from num2words import num2words
TEST_CASES_TO_CURRENCY_EUR = (
(1.00, 'ein Euro und null Cent'),
(2.01, 'zwei Euro und ein Cent'),
(8.10, 'acht Euro und zehn Cent'),
(12.26, 'zwölf Euro und sechsundzwanzig Cent'),
(21.29, 'einundzwanzig Euro und neunundzwanzig Cent'),
(81.25, 'einundachtzig Euro und fünfundzwanzig Cent'),
(100.00, 'einhundert Euro und null Cent'),
)
TEST_CASES_TO_CURRENCY_USD = (
(1.00, 'ein Dollar und null Cent'),
(2.01, 'zwei Dollar und ein Cent'),
(8.10, 'acht Dollar und zehn Cent'),
(12.26, 'zwölf Dollar und sechsundzwanzig Cent'),
(21.29, 'einundzwanzig Dollar und neunundzwanzig Cent'),
(81.25, 'einundachtzig Dollar und fünfundzwanzig Cent'),
(100.00, 'einhundert Dollar und null Cent'),
)
TEST_CASES_TO_CURRENCY_GBP = (
(1.00, 'ein Pfund und null Pence'),
(2.01, 'zwei Pfund und ein Penny'),
(8.10, 'acht Pfund und zehn Pence'),
(12.26, 'zwölf Pfund und sechsundzwanzig Pence'),
(21.29, 'einundzwanzig Pfund und neunundzwanzig Pence'),
(81.25, 'einundachtzig Pfund und fünfundzwanzig Pence'),
(100.00, 'einhundert Pfund und null Pence'),
)
TEST_CASES_TO_CURRENCY_DEM = (
(1.00, 'ein Mark und null Pfennig'),
(2.01, 'zwei Mark und ein Pfennig'),
(8.10, 'acht Mark und zehn Pfennig'),
(12.26, 'zwölf Mark und sechsundzwanzig Pfennig'),
(21.29, 'einundzwanzig Mark und neunundzwanzig Pfennig'),
(81.25, 'einundachtzig Mark und fünfundzwanzig Pfennig'),
(100.00, 'einhundert Mark und null Pfennig'),
)
class Num2WordsDETest(TestCase): class Num2WordsDETest(TestCase):
@@ -93,29 +133,33 @@ class Num2WordsDETest(TestCase):
def test_ordinal_for_floating_numbers(self): def test_ordinal_for_floating_numbers(self):
self.assertRaises(TypeError, num2words, 2.453, ordinal=True, lang='de') self.assertRaises(TypeError, num2words, 2.453, ordinal=True, lang='de')
def test_currency(self): def test_currency_eur(self):
self.assertEqual(num2words(1, lang='de', to='currency'), for test in TEST_CASES_TO_CURRENCY_EUR:
'ein Euro') self.assertEqual(
self.assertEqual(num2words(12, lang='de', to='currency'), num2words(test[0], lang='de', to='currency', currency='EUR'),
'zwölf Euro') test[1]
self.assertEqual(num2words(12.00, lang='de', to='currency'), )
'zwölf Euro')
self.assertEqual(num2words(100.0, lang='de', to='currency'),
"einhundert Euro")
self.assertEqual(num2words(190, lang='de', to='currency'),
"einhundertneunzig Euro")
self.assertEqual(num2words(1.90, lang='de', to='currency'),
"ein Euro und neunzig Cent")
self.assertEqual(num2words(3.4, lang='de', to='currency'),
"drei Euro und vierzig Cent")
self.assertEqual(num2words(20.18, lang='de', to='currency'),
"zwanzig Euro und achtzehn Cent")
self.assertEqual(num2words(3.04, lang='de', to='currency'),
"drei Euro und vier Cent")
def test_old_currency(self): def test_currency_usd(self):
self.assertEqual(num2words(12.00, to='currency', lang='de', old=True), for test in TEST_CASES_TO_CURRENCY_USD:
'zwölf Mark') self.assertEqual(
num2words(test[0], lang='de', to='currency', currency='USD'),
test[1]
)
def test_currency_dem(self):
for test in TEST_CASES_TO_CURRENCY_DEM:
self.assertEqual(
num2words(test[0], lang='de', to='currency', currency='DEM'),
test[1]
)
def test_currency_gbp(self):
for test in TEST_CASES_TO_CURRENCY_GBP:
self.assertEqual(
num2words(test[0], lang='de', to='currency', currency='GBP'),
test[1]
)
def test_year(self): def test_year(self):
self.assertEqual(num2words(2002, to='year', lang='de'), self.assertEqual(num2words(2002, to='year', lang='de'),

View File

@@ -118,26 +118,34 @@ TEST_CASES_ORDINAL_NUM = (
(1000000, '1000000me') (1000000, '1000000me')
) )
TEST_CASES_TO_CURRENCY = ( TEST_CASES_TO_CURRENCY_EUR = (
(1, 'un euro'), (1.00, 'un euro et zéro centimes'),
(2, 'deux euros'), (2.01, 'deux euros et un centime'),
(8, 'huit euros'), (8.10, 'huit euros et dix centimes'),
(12, 'douze euros'), (12.26, 'douze euros et vingt-six centimes'),
(21, 'vingt et un euros'), (21.29, 'vingt et un euros et vingt-neuf centimes'),
(81.25, 'quatre-vingt-un euros et vingt-cinq centimes'), (81.25, 'quatre-vingt-un euros et vingt-cinq centimes'),
(81.2, 'quatre-vingt-un euros et vingt centimes'), (100.00, 'cent euros et zéro centimes'),
(100, 'cent euros'),
) )
TEST_CASES_TO_CURRENCY_OLD = ( TEST_CASES_TO_CURRENCY_FRF = (
(1, 'un franc'), (1.00, 'un franc et zéro centimes'),
(2, 'deux francs'), (2.01, 'deux francs et un centime'),
(8, 'huit francs'), (8.10, 'huit francs et dix centimes'),
(12, 'douze francs'), (12.27, 'douze francs et vingt-sept centimes'),
(21, 'vingt et un francs'), (21.29, 'vingt et un francs et vingt-neuf centimes'),
(81.25, 'quatre-vingt-un francs et vingt-cinq centimes'), (81.25, 'quatre-vingt-un francs et vingt-cinq centimes'),
(81.2, 'quatre-vingt-un francs et vingt centimes'), (100.00, 'cent francs et zéro centimes'),
(100, 'cent francs'), )
TEST_CASES_TO_CURRENCY_USD = (
(1.00, 'un dollar et zéro cents'),
(2.01, 'deux dollars et un cent'),
(8.10, 'huit dollars et dix cents'),
(12.26, 'douze dollars et vingt-six cents'),
(21.29, 'vingt et un dollars et vingt-neuf cents'),
(81.25, 'quatre-vingt-un dollars et vingt-cinq cents'),
(100.00, 'cent dollars et zéro cents'),
) )
@@ -175,16 +183,23 @@ class Num2WordsENTest(TestCase):
test[1] test[1]
) )
def test_currency(self): def test_currency_eur(self):
for test in TEST_CASES_TO_CURRENCY: for test in TEST_CASES_TO_CURRENCY_EUR:
self.assertEqual( self.assertEqual(
num2words(test[0], lang='fr', to='currency'), num2words(test[0], lang='fr', to='currency', currency='EUR'),
test[1] test[1]
) )
def test_currency_old(self): def test_currency_frf(self):
for test in TEST_CASES_TO_CURRENCY_OLD: for test in TEST_CASES_TO_CURRENCY_FRF:
self.assertEqual( self.assertEqual(
num2words(test[0], lang='fr', to='currency', old=True), num2words(test[0], lang='fr', to='currency', currency='FRF'),
test[1]
)
def test_currency_usd(self):
for test in TEST_CASES_TO_CURRENCY_USD:
self.assertEqual(
num2words(test[0], lang='fr', to='currency', currency='USD'),
test[1] test[1]
) )

View File

@@ -54,24 +54,24 @@ TEST_CASES_ORDINAL = (
(1000000000000000000, 'un trillionsième') # over 1e18 is not supported (1000000000000000000, 'un trillionsième') # over 1e18 is not supported
) )
TEST_CASES_TO_CURRENCY = ( TEST_CASES_TO_CURRENCY_EUR = (
(1, 'un euro'), (1.00, 'un euro et zéro centimes'),
(2, 'deux euros'), (2.01, 'deux euros et un centime'),
(8, 'huit euros'), (8.10, 'huit euros et dix centimes'),
(12, 'douze euros'), (12.26, 'douze euros et vingt-six centimes'),
(21, 'vingt et un euros'), (21.29, 'vingt et un euros et vingt-neuf centimes'),
(81.25, 'quatre-vingt et un euros et vingt-cinq centimes'), (81.25, 'quatre-vingt et un euros et vingt-cinq centimes'),
(100, 'cent euros'), (100.00, 'cent euros et zéro centimes'),
) )
TEST_CASES_TO_CURRENCY_OLD = ( TEST_CASES_TO_CURRENCY_FRF = (
(1, 'un franc'), (1.00, 'un franc et zéro centimes'),
(2, 'deux francs'), (2.01, 'deux francs et un centime'),
(8, 'huit francs'), (8.10, 'huit francs et dix centimes'),
(12, 'douze francs'), (12.27, 'douze francs et vingt-sept centimes'),
(21, 'vingt et un francs'), (21.29, 'vingt et un francs et vingt-neuf centimes'),
(81.25, 'quatre-vingt et un francs et vingt-cinq centimes'), (81.25, 'quatre-vingt et un francs et vingt-cinq centimes'),
(100, 'cent francs'), (100.00, 'cent francs et zéro centimes'),
) )
# Lang to execute current test # Lang to execute current test
@@ -114,16 +114,16 @@ class Num2WordsENTest(TestCase):
test[1] test[1]
) )
def test_currency(self): def test_currency_eur(self):
for test in TEST_CASES_TO_CURRENCY: for test in TEST_CASES_TO_CURRENCY_EUR:
self.assertEqual( self.assertEqual(
num2words(test[0], lang=LANG, to='currency'), num2words(test[0], lang=LANG, to='currency'),
test[1] test[1]
) )
def test_currency_old(self): def test_currency_frf(self):
for test in TEST_CASES_TO_CURRENCY_OLD: for test in TEST_CASES_TO_CURRENCY_FRF:
self.assertEqual( self.assertEqual(
num2words(test[0], lang=LANG, to='currency', old=True), num2words(test[0], lang=LANG, to='currency', currency='FRF'),
test[1] test[1]
) )

View File

@@ -55,24 +55,24 @@ TEST_CASES_ORDINAL = (
(1000000000000000000, 'un trillionsième') # over 1e18 is not supported (1000000000000000000, 'un trillionsième') # over 1e18 is not supported
) )
TEST_CASES_TO_CURRENCY = ( TEST_CASES_TO_CURRENCY_EUR = (
(1, 'un euro'), (1.00, 'un euro et zéro centimes'),
(2, 'deux euros'), (2.01, 'deux euros et un centime'),
(8, 'huit euros'), (8.10, 'huit euros et dix centimes'),
(12, 'douze euros'), (12.26, 'douze euros et vingt-six centimes'),
(21, 'vingt et un euros'), (21.29, 'vingt et un euros et vingt-neuf centimes'),
(81.25, 'huitante et un euros et vingt-cinq centimes'), (81.25, 'huitante et un euros et vingt-cinq centimes'),
(100, 'cent euros'), (100.00, 'cent euros et zéro centimes'),
) )
TEST_CASES_TO_CURRENCY_OLD = ( TEST_CASES_TO_CURRENCY_FRF = (
(1, 'un franc'), (1.00, 'un franc et zéro centimes'),
(2, 'deux francs'), (2.01, 'deux francs et un centime'),
(8, 'huit francs'), (8.10, 'huit francs et dix centimes'),
(12, 'douze francs'), (12.27, 'douze francs et vingt-sept centimes'),
(21, 'vingt et un francs'), (21.29, 'vingt et un francs et vingt-neuf centimes'),
(81.25, 'huitante et un francs et vingt-cinq centimes'), (81.25, 'huitante et un francs et vingt-cinq centimes'),
(100, 'cent francs'), (100.00, 'cent francs et zéro centimes'),
) )
@@ -111,16 +111,17 @@ class Num2WordsENTest(TestCase):
test[1] test[1]
) )
def test_currency(self): def test_currency_eur(self):
for test in TEST_CASES_TO_CURRENCY: for test in TEST_CASES_TO_CURRENCY_EUR:
self.assertEqual( self.assertEqual(
num2words(test[0], lang='fr_CH', to='currency'), num2words(test[0], lang='fr_CH', to='currency'),
test[1] test[1]
) )
def test_currency_old(self): def test_currency_frf(self):
for test in TEST_CASES_TO_CURRENCY_OLD: for test in TEST_CASES_TO_CURRENCY_FRF:
self.assertEqual( self.assertEqual(
num2words(test[0], lang='fr_CH', to='currency', old=True), num2words(test[0], lang='fr_CH', to='currency',
currency='FRF'),
test[1] test[1]
) )

View File

@@ -29,7 +29,7 @@ TEST_CASES_TO_CURRENCY = (
(8, 'huit dinards'), (8, 'huit dinards'),
(12, 'douze dinards'), (12, 'douze dinards'),
(21, 'vingt et un dinards'), (21, 'vingt et un dinards'),
(81.25, 'quatre-vingt-un dinards virgule vingt-cinq centimes'), (81.25, 'quatre-vingt-un dinards et vingt-cinq centimes'),
(100, 'cent dinards'), (100, 'cent dinards'),
) )
@@ -38,11 +38,11 @@ class Num2WordsPLTest(TestCase):
def test_currency(self): def test_currency(self):
self.assertEqual( self.assertEqual(
num2words(1234.12, lang='fr_DZ', to='currency'), num2words(1234.12, lang='fr_DZ', to='currency'),
"mille deux cent trente-quatre dinards virgule douze centimes" "mille deux cent trente-quatre dinards et douze centimes"
) )
self.assertEqual( self.assertEqual(
num2words(45689.89, lang='fr_DZ', to='currency'), num2words(45689.89, lang='fr_DZ', to='currency'),
"quarante-cinq mille six cent quatre-vingt-neuf dinards virgule " "quarante-cinq mille six cent quatre-vingt-neuf dinards et "
"quatre-vingt-neuf centimes" "quatre-vingt-neuf centimes"
) )

View File

@@ -67,7 +67,7 @@ class Num2WordsNLTest(TestCase):
def test_ordinal_for_floating_numbers(self): def test_ordinal_for_floating_numbers(self):
self.assertRaises(TypeError, num2words, 2.453, ordinal=True, lang='nl') self.assertRaises(TypeError, num2words, 2.453, ordinal=True, lang='nl')
def test_to_currency(self): def test_to_currency_eur(self):
self.assertEqual( self.assertEqual(
num2words('38.4', lang='nl', to='currency', separator=' en', num2words('38.4', lang='nl', to='currency', separator=' en',
cents=False, currency='EUR'), cents=False, currency='EUR'),
@@ -90,6 +90,29 @@ class Num2WordsNLTest(TestCase):
cents=True, currency='EUR'), cents=True, currency='EUR'),
'vierduizendzevenhonderdachtenzeventig euro en nul cent') 'vierduizendzevenhonderdachtenzeventig euro en nul cent')
def test_to_currency_usd(self):
self.assertEqual(
num2words('38.4', lang='nl', to='currency', separator=' en',
cents=False, currency='USD'),
"achtendertig dollar en 40 cent"
)
self.assertEqual(
num2words('0', lang='nl', to='currency', separator=' en',
cents=False, currency='USD'),
"nul dollar en 00 cent"
)
self.assertEqual(
num2words('1.01', lang='nl', to='currency', separator=' en',
cents=True, currency='USD'),
"één dollar en één cent"
)
self.assertEqual(
num2words('4778.00', lang='nl', to='currency', separator=' en',
cents=True, currency='USD'),
'vierduizendzevenhonderdachtenzeventig dollar en nul cent')
def test_pluralize(self): def test_pluralize(self):
n = Num2Word_NL() n = Num2Word_NL()
# euros always singular # euros always singular