我已经检查了num2words库,但我们想要数字,所以我们可以在支票上打印。 恩。 23.25意味着"二十三和二十五美分"在num2words库中对待"二十三点二五"
答案 0 :(得分:2)
试试这个:
from num2words import num2words
test = 23.25
intpart,decimalpart = int(test), test-int(test)
print(num2words(intpart).replace('-', ' ') + ' and ' + str( int(decimalpart * (10 ** (len(str(decimalpart)) - 2)))) + ' cent')
答案 1 :(得分:0)
您始终可以扩展现有库:)
from num2words import num2words
def amt2words(amount, currency='dollars', change='cents', precision=2):
change_amt = (amount - int(amount))*pow(10, precision)
words = '{main_amt} {main_word}'.format(
main_amt=num2words(int(amount)),
main_word=currency,
)
if change_amt > 0:
words += ' and {change_amt} {change_word}'.format(
change_amt=num2words(change_amt),
change_word=change,
)
return words
amt2words(23.25)
# 'twenty-three dollars and twenty-five cents'
amt2words(20)
# 'twenty dollars'