如何将印度语中的金额转换为单词?
我正在使用 num2words 库,但它在呈现“十万”和“千万”时呈现错误的单词集。
例如:
num2words(903614.55, lang='en-IN')
其印刷'nine hundred and three thousand, six hundred and fourteen point five five'
但实际的印度金额表述应该是nine lakhs three thousand six hundred fourteen and five five paisa
.
然后我尝试了下面的代码:
def num2words(num):
under_20 = ['Zero','One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen']
tens = ['Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety']
above_100 = {100: 'Hundred',1000:'Thousand', 100000:'Lakhs', 10000000:'Crores'}
if num < 20:
return under_20[num]
if num < 100:
return tens[(int)(num/10)-2] + ('' if num%10==0 else ' ' + under_20[num%10])
# find the appropriate pivot - 'Million' in 3,603,550, or 'Thousand' in 603,550
pivot = max([key for key in above_100.keys() if key <= num])
return num2words((int)(num/pivot)) + ' ' + above_100[pivot] + ('' if num%pivot==0 else ' ' + num2words(num%pivot))
但现在错误来了
TypeError:列表索引必须是整数或切片,而不是小数。
我的问题是小数,整数工作正常。