人民币阿拉伯数字转中文大写的几种特殊情况
- 连着多个‘0’读作一个“零”
- 开头数字是‘1’时省略“壹”,如‘11’读作“拾壹”而不是“壹拾壹”
- 小数为‘00’时读作“整”
- 小数为‘0X’时读作“X分”
- 小数为‘X0’时读锁“X角”
- 小数为‘XX’时读作“X角X分”
- 整数部分仅为‘1’时,‘壹’不省略
- 整数部分为‘0’时,直接读小数,省略“元”
华为机试人民币转换
import re
while True:
try:
al = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"]
de = ["元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万亿", "拾", "佰", "仟"]
res = '人民币'
shu = input().split('.')
data = shu[0]
if shu[0][0] == '0':
res = res
elif shu[0][0] == '1' and len(shu[0]) == 1:
res = res + "壹" + "元"
else:
for i in range(len(data)):
if len(res)>0 and res[-1] == "零" and data[i] == '0':
continue
elif data[i] == '0':
res = res + al[int(data[i])]
elif i == 0 and data[i] == '1':
res = res + de[len(data) - i - 1]
else:
res = res + al[int(data[i])] + de[len(data)-i-1]
data = shu[1]
if data[0] != '0' and data[1] == '0':
res = res + al[int(data[0])] + '角'
elif data[0] == '0' and data[1] != '0':
if shu[0][0] == '0':
res = res + al[int(data[1])] + '分'
else:
res = res + al[int(data[1])] + '分'
elif data[0] != '0' and data[1] != '0':
res = res + al[int(data[0])] + '角' + al[int(data[1])] + '分'
else:
res = res + '整'
print(res)
except:
break