大写金额转小写(千万以下)

这里使用字典来保存大小写的对应关系,其中“万”、“零”、“整”是特殊字符,需要判断处理

def hantonum(str1):
dict1 = {'一': 1, '二': 2, '三': 3, '四': 4, '五': 5, '六': 6, '七': 7, '八': 8, '九': 9}
dict2 = {'十': 10, '百': 100, '千': 1000, '万': 10000, '元': 1, '角': 0.1, '分': 0.01}
result = 0
for index,i in enumerate(str1):
if index<len(str1)-1:
if (i in dict1 and str1[index+1] in dict2) or (i in dict2 and str1[index+1]=='万'):
if str1[index+1] !='万':
result += dict1[i] * dict2[str1[index+1]]
elif i in dict2:
result *=10000
else:
result += dict1[i]
result *= 10000
return result

print(hantonum('一千九百八十五万八千六百二十九元三角二分'))
print(hantonum('一千九百三十万八千六百二十八元三角二分'))
print(hantonum('一千九百零三万八千六百二十九元整'))


原文地址:https://www.cnblogs.com/sunmingduo/p/10466176.html