在Python中将金额转换为单词

时间:2015-06-07 03:32:10

标签: python

你能帮助我用西班牙语将金额换成单词吗? 我使用的是num2word库,但你可以看到一些未正确转换的小数点。

>>> from num2words import num2words
>>> num2words(9036.20)
u'nine thousand and thirty-six point two zero'
>>> num2words(9036.21)
u'nine thousand and thirty-six point two zero'
>>> num2words(9036.55)
u'nine thousand and thirty-six point five four'
>>> num2words(9036.55, lang='es')
u'nueve mil treinta y seis punto cinco cuatro'

请分享您的宝贵经验。

1 个答案:

答案 0 :(得分:4)

暂且不说:Stack Overflow的西班牙语is now in the public beta stage

This is a known issue with num2word,解决方法是使用Decimal并将浮点值转换为字符串

>>> from num2words import num2words
>>> from decimal import Decimal
>>> num2words(Decimal(str(9036.55)), lang='es')
>>> u'nueve mil treinta y seis punto cinco cinco'