- 像这种对应关系比较多的话,可以使用字典数据,减少if 的判断。
amount_chinese = {1: '壹', 2: '贰', 3: '叁', 4: '肆', 5: '伍', 6: '陆', 7: '柒', 8: '捌', 9: '玖', 10: '拾', 100: '佰', 1000: '仟',
10000: '万', 100000000: '亿', 15: '元', 16: '角', 17: '分', 0: '零', 19: '整'}
place = {1: 1, 2: 10, 3: 100, 4: 1000}
def write_to_chinese(num):
new_str = ""
num_len = len(str(num))
while True:
if num_len == 1:
if num != 0:
new_str += amount_chinese.get(num)
break
if num % place.get(num_len) == 0:
new_str += amount_chinese.get(int(str(num)[0])) + amount_chinese.get(num)
break
if num_len in place:
new_str += amount_chinese.get(num // place.get(num_len))
position = amount_chinese.get(place.get(num_len))
new_str += position
num = num % place.get(num_len)
if num_len - len(str(num)) > 1:
new_str += amount_chinese.get(0)
num_len = len(str(num))
return new_str
while True:
try:
num_list = input().split(".")
new_num = 0
string = ""
point_num = None
if len(num_list) == 2:
point_num = num_list[1]
num = int(num_list[0])
if 15>=len(str(num)) >12:
new_num = num // 100000000000
num = num % 100000000000
string += write_to_chinese(new_num)
string += amount_chinese.get(10000)
string += amount_chinese.get(100000000)
if 13>len(str(num)) >=9:
new_num = num // 100000000
num = num % 100000000
string += write_to_chinese(new_num)
string += amount_chinese.get(100000000)
if 9 > len(str(num)) >= 5:
new_num = num // 10000
num = num % 10000
string += write_to_chinese(new_num)
string += amount_chinese.get(10000)
string += write_to_chinese(num)
if not point_num:
string += amount_chinese.get(15) + amount_chinese.get(19)
elif len(point_num) == 2 and point_num[0] == point_num[1] =="0":
string += amount_chinese.get(19)
else:
if num !=0:
string += amount_chinese.get(15)
for i in range(len(point_num)):
if point_num[i] =="0":
continue
string += amount_chinese.get(int(point_num[i]))
if i ==0:
string += amount_chinese.get(16)
else:
string += amount_chinese.get(17)
string = string.replace("壹拾","拾")
print("人民币"+string)
except:
break