kollector
New member
Python function that converts a string of a decimal(negative or positive) in digit form to it's word representation
Python:
import inflect
def decimal_to_words(decimal):
p = inflect.engine()
if '.' in decimal:
if decimal.startswith('-'):
integer_part, decimal_part = decimal[1:].split('.')
words = 'negative ' + p.number_to_words(integer_part) + ' point ' + ' '.join(p.number_to_words(i) for i in decimal_part)
else:
integer_part, decimal_part = decimal.split('.')
words = p.number_to_words(integer_part) + ' point ' + ' '.join(p.number_to_words(i) for i in decimal_part)
else:
words = p.number_to_words(decimal)
return words
# Example usage:
print(decimal_to_words("-123.45"))