Skip to content

Commit

Permalink
Convert strings to Decimal values
Browse files Browse the repository at this point in the history
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
  • Loading branch information
btharper committed Nov 1, 2018
1 parent 5c9bce1 commit 446e918
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
2 changes: 2 additions & 0 deletions num2words/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions num2words/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ def parse_minus(self, num_str):
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
Expand Down
4 changes: 2 additions & 2 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def test_cli_default_lang(self):
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):
Expand All @@ -82,7 +82,7 @@ def test_cli_with_lang(self):
self.assertEqual(output.return_code, 0)
self.assertEqual(
output.out.strip(),
"ciento cincuenta punto cero"
"ciento cincuenta"
)

def test_cli_with_lang_to(self):
Expand Down

0 comments on commit 446e918

Please sign in to comment.