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

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('一千九百零三万八千六百二十九元整'))


相关文章:

  • 2022-01-19
  • 2021-09-19
  • 2021-12-04
  • 2021-06-30
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-01-08
  • 2021-06-01
  • 2021-08-06
  • 2021-06-24
  • 2022-12-23
  • 2021-05-14
相关资源
相似解决方案