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
This commit is contained in:
btharper
2018-10-31 22:01:26 -04:00
parent 5c9bce19e4
commit 446e918e14
3 changed files with 7 additions and 2 deletions

View File

@@ -96,6 +96,8 @@ def num2words(number, ordinal=False, lang='en', to='cardinal', **kwargs):
if lang not in CONVERTER_CLASSES: if lang not in CONVERTER_CLASSES:
raise NotImplementedError() raise NotImplementedError()
converter = CONVERTER_CLASSES[lang] converter = CONVERTER_CLASSES[lang]
if isinstance(number, str):
number = converter.str_to_number(number)
# backwards compatible # backwards compatible
if ordinal: if ordinal:
return converter.to_ordinal(number) return converter.to_ordinal(number)

View File

@@ -96,6 +96,9 @@ class Num2Word_Base(object):
return '%s ' % self.negword, num_str[1:] return '%s ' % self.negword, num_str[1:]
return '', num_str return '', num_str
def str_to_number(self, value):
return Decimal(value)
def to_cardinal(self, value): def to_cardinal(self, value):
try: try:
assert int(value) == value assert int(value) == value

View File

@@ -72,7 +72,7 @@ class CliTestCase(unittest.TestCase):
self.assertEqual(output.return_code, 0) self.assertEqual(output.return_code, 0)
self.assertEqual( self.assertEqual(
output.out.strip(), output.out.strip(),
"one hundred and fifty point zero" "one hundred and fifty"
) )
def test_cli_with_lang(self): def test_cli_with_lang(self):
@@ -82,7 +82,7 @@ class CliTestCase(unittest.TestCase):
self.assertEqual(output.return_code, 0) self.assertEqual(output.return_code, 0)
self.assertEqual( self.assertEqual(
output.out.strip(), output.out.strip(),
"ciento cincuenta punto cero" "ciento cincuenta"
) )
def test_cli_with_lang_to(self): def test_cli_with_lang_to(self):