例如我输入123可以输出壹佰贰拾叁
并且可以输出的是亿以内的
中间不能有零佰零仟的无效表达
def digital_to_chinese(digital):
str_digital = str(digital)
chinese = {'1': '壹', '2': '贰', '3': '叁', '4': '肆', '5': '伍', '6': '陆', '7': '柒', '8': '捌', '9': '玖', '0': '零'}
chinese2 = ['拾', '佰', '仟', '万', '厘', '分', '角']
jiao = ''
bs = str_digital.split('.')
yuan = bs[0]
if len(bs) > 1:
jiao = bs[1]
r_yuan = [i for i in reversed(yuan)]
count = 0
for i in range(len(yuan)):
if i == 0:
r_yuan[i] += '圆'
continue
r_yuan[i] += chinese2[count]
count += 1
if count == 4:
count = 0
chinese2[3] = '亿'
s_jiao = [i for i in jiao][:3] # 去掉小于厘之后的
j_count = -1
for i in range(len(s_jiao)):
s_jiao[i] += chinese2[j_count]
j_count -= 1
last = [i for i in reversed(r_yuan)] + s_jiao
last_str = ''.join(last)
print(str_digital)
print(last_str)
for i in range(len(last_str)):
digital = last_str[i]
if digital in chinese:
last_str = last_str.replace(digital, chinese[digital])
print(last_str)
return last_str
if __name__ == '__main__':
digital_to_chinese(123456789.456)
设置一个字典映射变量,再加一些条件判断就可以了。
def switch_num(num):
"""把数字转换成中文"""
if type(num) != int:
return None
elif num == 1:
return "壹"
elif num == 2:
return "贰"
elif num == 3:
return "叁"
elif num == 4:
return "肆"
elif num == 5:
return "伍"
elif num == 6:
return "陆"
elif num == 7:
return "柒"
elif num == 8:
return "捌"
elif num == 9:
return "玖"
# 判断位数并转换,//运算符表示除法运算后向下取整, %运算符表示模运算,即除法运算后取余数
while True:
try:
n = int(input("输入大于或等于0且位数不大于9位的数字:"))
print(n)
if n > 0:
if len(str(n)) == 9:
a = n // 100000000
a = switch_num(a) + "亿"
b = n // 10000000 % 10
b = switch_num(b) + "千"
c = n // 1000000 % 10
c = switch_num(c) + "佰"
d = n // 100000 % 10
d = switch_num(d) + "拾"
e = n // 10000 % 10
e = switch_num(e) + "万"
f = n // 1000 % 10
f = switch_num(f) + "仟"
g = n // 100 % 10
g = switch_num(g) + "佰"
h = n // 10 % 10
h = switch_num(h) + "十"
i = n // 1 % 10
i = switch_num(i)
print(a + b + c + d + e + f + g + h + i)
elif len(str(n)) == 8:
b = n // 10000000 % 10
b = switch_num(b) + "千"
c = n // 1000000 % 10
c = switch_num(c) + "佰"
d = n // 100000 % 10
d = switch_num(d) + "拾"
e = n // 10000 % 10
e = switch_num(e) + "万"
f = n // 1000 % 10
f = switch_num(f) + "仟"
g = n // 100 % 10
g = switch_num(g) + "佰"
h = n // 10 % 10
h = switch_num(h) + "十"
i = n // 1 % 10
i = switch_num(i)
print(b + c + d + e + f + g + h + i)
elif len(str(n)) == 7:
c = n // 1000000 % 10
c = switch_num(c) + "佰"
d = n // 100000 % 10
d = switch_num(d) + "拾"
e = n // 10000 % 10
e = switch_num(e) + "万"
f = n // 1000 % 10
f = switch_num(f) + "仟"
g = n // 100 % 10
g = switch_num(g) + "佰"
h = n // 10 % 10
h = switch_num(h) + "十"
i = n // 1 % 10
i = switch_num(i)
print(c + d + e + f + g + h + i)
elif len(str(n)) == 6:
d = n // 100000 % 10
d = switch_num(d) + "拾"
e = n // 10000 % 10
e = switch_num(e) + "万"
f = n // 1000 % 10
f = switch_num(f) + "仟"
g = n // 100 % 10
g = switch_num(g) + "佰"
h = n // 10 % 10
h = switch_num(h) + "十"
i = n // 1 % 10
i = switch_num(i)
print(d + e + f + g + h + i)
elif len(str(n)) == 5:
e = n // 10000 % 10
e = switch_num(e) + "万"
f = n // 1000 % 10
f = switch_num(f) + "仟"
g = n // 100 % 10
g = switch_num(g) + "佰"
h = n // 10 % 10
h = switch_num(h) + "十"
i = n // 1 % 10
i = switch_num(i)
print(e + f + g + h + i)
elif len(str(n)) == 4:
f = n // 1000 % 10
f = switch_num(f) + "仟"
g = n // 100 % 10
g = switch_num(g) + "佰"
h = n // 10 % 10
h = switch_num(h) + "十"
i = n // 1 % 10
i = switch_num(i)
print(f + g + h + i)
elif len(str(n)) == 3:
g = n // 100 % 10
g = switch_num(g) + "佰"
h = n // 10 % 10
h = switch_num(h) + "十"
i = n // 1 % 10
i = switch_num(i)
print(g + h + i)
elif len(str(n)) == 2:
h = n // 10 % 10
h = switch_num(h) + "十"
i = n // 1 % 10
i = switch_num(i)
print(h + i)
elif len(str(n)) == 1:
i = n // 1 % 10
i = switch_num(i)
print(i)
else:
print("请输入不超过9位的数字!")
else:
print("输入错误,请输入大于0的数字!")
except:
print("输入错误,请输入数字!")