对中间有多个0的处理方式,对末尾有0的处理方式的一些思考。
以上,供参考
def func(x)->str: res = [] zero_pre = False # 确保在连续‘0’的情况下,只有一个'零' for i, xi in enumerate(x[::-1]): # 倒着转化 if xi=='0': if not zero_pre: res.append(num[int(xi)]) zero_pre = True else: zero_pre = False res.extend([pro[i], num[int(xi)]]) res = res[::-1] # print(res) while res[-1]=='零':# 去掉后面所有的 ”零“ res.pop() return ''.join(res).replace('壹拾', '拾') num = ['零','壹','贰','叁','肆','伍','陆','柒','捌','玖'] pro = ['', '拾','佰','仟','万','亿','元','角','分','整'] while True: try: n, m = input().strip().split('.') res = ['人民币'] if len(n)>8: res.append(func(n[:-8])+'亿') if len(n)>4: res.append(func(n[-8:-4])+'万') if n!='0': res.append(func(n[-4:])+'元') if m=='00': res.append('整') else: if m[0]>'0': res.append(num[int(m[0])]+'角') if m[1]>'0': res.append(num[int(m[1])]+'分') print(''.join(res)) except: break """ 0.85 0.25 人民币捌角伍分 人民币贰角伍分 1024.56 10012.02 100110.00 30105000.00 人民币壹仟零贰拾肆元伍角陆分 人民币壹万零拾贰元贰分 人民币拾万零壹佰拾元整 人民币叁仟零拾万伍仟元整 """