You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.2 KiB

5 years ago
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
number = float(input("输入需要转换的数字:"))
if __name__ == '__main__':
digital_to_chinese(number)