Fix lang_IT handling of floats (#143)

`n % 1 != 0` is not a valid test for float:

In []: 1.1 % 1 == 0
Out[]: False

but:

In []: 1.0 % 1 == 0
Out[]: True

hence it's really necessary to explicitly test for type in this case.
This commit is contained in:
Carli Samuele
2017-12-04 23:16:06 +01:00
committed by Ernesto Rodriguez Ortiz
parent 8ffdc5e49d
commit efce631944
2 changed files with 9 additions and 2 deletions

View File

@@ -169,7 +169,7 @@ class Num2Word_IT:
def to_cardinal(self, number): def to_cardinal(self, number):
if number < 0: if number < 0:
string = Num2Word_IT.MINUS_PREFIX_WORD + self.to_cardinal(-number) string = Num2Word_IT.MINUS_PREFIX_WORD + self.to_cardinal(-number)
elif number % 1 != 0: elif isinstance(number, float):
string = self.float_to_words(number) string = self.float_to_words(number)
elif number < 20: elif number < 20:
string = CARDINAL_WORDS[number] string = CARDINAL_WORDS[number]

View File

@@ -22,7 +22,6 @@ from num2words import num2words
class Num2WordsITTest(TestCase): class Num2WordsITTest(TestCase):
maxDiff = None maxDiff = None
def test_negative(self): def test_negative(self):
@@ -225,3 +224,11 @@ class Num2WordsITTest(TestCase):
"novecentouno miliardi, duecentotrentaquattro milioni e " "novecentouno miliardi, duecentotrentaquattro milioni e "
"cinquecentosessantasettemilaottocentonovantesimo" "cinquecentosessantasettemilaottocentonovantesimo"
) )
def test_with_decimals(self):
self.assertAlmostEqual(
num2words(1.0, lang="it"), "uno virgola zero"
)
self.assertAlmostEqual(
num2words(1.1, lang="it"), "uno virgola uno"
)