Skip to content

Commit

Permalink
[it] Handle string inputs in to_ordinal
Browse files Browse the repository at this point in the history
Fixes #508.
  • Loading branch information
Quuxplusone committed May 3, 2023
1 parent da48a31 commit 87def57
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
12 changes: 6 additions & 6 deletions num2words/lang_IT.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,13 @@ def to_cardinal(self, number):
elif isinstance(number, float):
string = self.float_to_words(number)
elif number < 20:
string = CARDINAL_WORDS[number]
string = CARDINAL_WORDS[int(number)]
elif number < 100:
string = self.tens_to_cardinal(number)
string = self.tens_to_cardinal(int(number))
elif number < 1000:
string = self.hundreds_to_cardinal(number)
string = self.hundreds_to_cardinal(int(number))
elif number < 1000000:
string = self.thousands_to_cardinal(number)
string = self.thousands_to_cardinal(int(number))
else:
string = self.big_number_to_cardinal(number)
return accentuate(string)
Expand All @@ -167,9 +167,9 @@ def to_ordinal(self, number):
elif number % 1 != 0:
return self.float_to_words(number, ordinal=True)
elif number < 20:
return ORDINAL_WORDS[number]
return ORDINAL_WORDS[int(number)]
elif is_outside_teens and tens % 10 == 3:
# Gets ride of the accent ~~~~~~~~~~
# Gets rid of the accent
return self.to_cardinal(number)[:-1] + "eesimo"
elif is_outside_teens and tens % 10 == 6:
return self.to_cardinal(number) + "esimo"
Expand Down
25 changes: 24 additions & 1 deletion tests/test_it.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,14 +263,37 @@ def test_nth_big(self):
"cinquecentosessantasettemilaottocentonovantesimo"
)

def test_with_decimals(self):
def test_with_floats(self):
self.assertAlmostEqual(
num2words(1.0, lang="it"), "uno virgola zero"
)
self.assertAlmostEqual(
num2words(1.1, lang="it"), "uno virgola uno"
)

def test_with_strings(self):
for i in range(2002):
# Just make sure it doesn't raise an exception
num2words(str(i), lang='it', to='cardinal')
num2words(str(i), lang='it', to='ordinal')
self.assertEqual(num2words('1', lang="it", to='ordinal'), "primo")
self.assertEqual(
num2words('100', lang="it", to='ordinal'),
"centesimo"
)
self.assertEqual(
num2words('1000', lang="it", to='ordinal'),
"millesimo"
)
self.assertEqual(
num2words('1234567890123456789012345678', lang="it", to='ordinal'),
"un quadriliardo, duecentotrentaquattro quadrilioni, "
"cinquecentosessantasette triliardi, ottocentonovanta trilioni, "
"centoventitré biliardi, quattrocentocinquantasei bilioni, "
"settecentottantanove miliardi, dodici milioni e "
"trecentoquarantacinquemilaseicentosettantottesimo"
)

def test_currency_eur(self):
for test in TEST_CASES_TO_CURRENCY_EUR:
self.assertEqual(
Expand Down

0 comments on commit 87def57

Please sign in to comment.