From 446e918e1474dd9647caf83b488b0cef35c3327d Mon Sep 17 00:00:00 2001 From: btharper Date: Wed, 31 Oct 2018 22:01:26 -0400 Subject: [PATCH] Convert strings to Decimal values Punt string handling to python Decimal object, this correctly represents both integers and floats (except with regards to trailing zeros) Change command line tests to reflect handling of ints --- num2words/__init__.py | 2 ++ num2words/base.py | 3 +++ tests/test_cli.py | 4 ++-- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/num2words/__init__.py b/num2words/__init__.py index aaf40df..a2c18b6 100644 --- a/num2words/__init__.py +++ b/num2words/__init__.py @@ -96,6 +96,8 @@ def num2words(number, ordinal=False, lang='en', to='cardinal', **kwargs): if lang not in CONVERTER_CLASSES: raise NotImplementedError() converter = CONVERTER_CLASSES[lang] + if isinstance(number, str): + number = converter.str_to_number(number) # backwards compatible if ordinal: return converter.to_ordinal(number) diff --git a/num2words/base.py b/num2words/base.py index 3b990e3..c73f741 100644 --- a/num2words/base.py +++ b/num2words/base.py @@ -96,6 +96,9 @@ class Num2Word_Base(object): return '%s ' % self.negword, num_str[1:] return '', num_str + def str_to_number(self, value): + return Decimal(value) + def to_cardinal(self, value): try: assert int(value) == value diff --git a/tests/test_cli.py b/tests/test_cli.py index 3e1d3ab..638331a 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -72,7 +72,7 @@ class CliTestCase(unittest.TestCase): self.assertEqual(output.return_code, 0) self.assertEqual( output.out.strip(), - "one hundred and fifty point zero" + "one hundred and fifty" ) def test_cli_with_lang(self): @@ -82,7 +82,7 @@ class CliTestCase(unittest.TestCase): self.assertEqual(output.return_code, 0) self.assertEqual( output.out.strip(), - "ciento cincuenta punto cero" + "ciento cincuenta" ) def test_cli_with_lang_to(self):