Skip to content

Commit

Permalink
Merge pull request #223 from btharper/strings
Browse files Browse the repository at this point in the history
Convert strings to Decimal values
  • Loading branch information
kkonieczny committed Jan 18, 2019
2 parents 83cfcdc + e459021 commit c1292c1
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 @@ -101,6 +101,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 @@ -97,6 +97,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 @@ -87,7 +87,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 @@ -97,7 +97,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 c1292c1

Please sign in to comment.