From 87def57c4a747ee50009f79ad9fd6ca4a40fa138 Mon Sep 17 00:00:00 2001 From: Arthur O'Dwyer Date: Mon, 1 May 2023 16:36:42 -0400 Subject: [PATCH] [it] Handle string inputs in `to_ordinal` Fixes #508. --- num2words/lang_IT.py | 12 ++++++------ tests/test_it.py | 25 ++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/num2words/lang_IT.py b/num2words/lang_IT.py index d532f7f4..427e6d36 100644 --- a/num2words/lang_IT.py +++ b/num2words/lang_IT.py @@ -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) @@ -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" diff --git a/tests/test_it.py b/tests/test_it.py index 9023355f..23c081d1 100644 --- a/tests/test_it.py +++ b/tests/test_it.py @@ -263,7 +263,7 @@ 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" ) @@ -271,6 +271,29 @@ def test_with_decimals(self): 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(