From efce6319445cc3e786832564f37aebb7df7c9cff Mon Sep 17 00:00:00 2001 From: Carli Samuele Date: Mon, 4 Dec 2017 23:16:06 +0100 Subject: [PATCH] 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. --- num2words/lang_IT.py | 2 +- tests/test_it.py | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/num2words/lang_IT.py b/num2words/lang_IT.py index e4c9273..3b223c3 100644 --- a/num2words/lang_IT.py +++ b/num2words/lang_IT.py @@ -169,7 +169,7 @@ class Num2Word_IT: def to_cardinal(self, number): if number < 0: string = Num2Word_IT.MINUS_PREFIX_WORD + self.to_cardinal(-number) - elif number % 1 != 0: + elif isinstance(number, float): string = self.float_to_words(number) elif number < 20: string = CARDINAL_WORDS[number] diff --git a/tests/test_it.py b/tests/test_it.py index a94f63f..d358bac 100644 --- a/tests/test_it.py +++ b/tests/test_it.py @@ -22,7 +22,6 @@ from num2words import num2words class Num2WordsITTest(TestCase): - maxDiff = None def test_negative(self): @@ -225,3 +224,11 @@ class Num2WordsITTest(TestCase): "novecentouno miliardi, duecentotrentaquattro milioni e " "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" + )