mirror of
https://github.com/bblaz/num2words.git
synced 2025-12-06 14:52:25 +00:00
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:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user