py浮点型运算有一个魔法,例如1.01*3.0=3.0300000000000002
所以全部转为整数运算,最后除100返回
def toInt(value):
isum = 0
nums = {'零': 0, '壹': 1, '贰':2, '叁': 3, '肆': 4, '伍': 5, '陆': 6, '柒': 7, '捌': 8, '玖':9}
dw = {'拾': 3, '元': 2, '佰': 4, '仟': 5, '万' : 6, '角': 1, '分': 0}
index = 0
while index < len(value):
if value[index] != '零':
if (value[index] == '拾'):
isum += 1000;
index += 1
elif value[index] in nums :
isum += nums[value[index]] * pow(10, dw[value[index + 1]])
index += 2
else:
index +=1
else:
index += 1
return round(isum, 2) / 100
#输入一个整数,输出其对应的汉字表示数字
n = int(input())
numl = ['零','一','二','三','四','五','六','七','八','九']
dw = ['', '十', '百', '千', '万', '十', '百', '千', '亿']
ret = ""
t = ""
w = 0
hava = False
d = ''
while n > 0:
d = numl[n % 10]
if d[0] != '零':
d += dw[w % 4]
t = d + t
if t == '一十':#处理特殊情况
t = '十'
if w % 4 == 0:#处理尾零
while t != "" and t[-1] == '零':
t = t[0:len(t) - 1]
t += dw[w]
wl = False
ret = t + ret
t = ""
n //= 10
w += 1
#处理连续的零
for i in range(1, len(ret) - 1):
if i >= len(ret):
break
if ret[i] == '零' and ret[i - 1] == '零':
d = ret[0:i - 1] + ret[i + 1:]
i -= 1
ret = d
print(ret)
'''
60005082
六千万五千零八十二
100002
十万零二
67665782
六千七百六十六万五千七百八十二
'''
发表回复