chinese_list = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
unit_list = ['十', '百', '千']
decimal_list = ['角', '分']
def divide_str(num):
int_num = int(num)
float_num = round((num - int_num) * 100)
return (str(int_num), str(float_num))
def four_to_handstr(num_str):
result = ""
num_len = len(num_str)
for i in range(num_len):
num = int(num_str[i])
if i != num_len - 1 and num != 0:
result += chinese_list[num] + unit_list[num_len - 2 - i]
elif i != num_len - 1 and num_str[i] == "0" and num_str[i + 1] == "0":
continue
elif i == num_len - 1 and num_str[i] == '0':
continue
elif i == num_len - 1 and num_str[i] == "0" and num_str[i + 1] == "0":
continue
else:
result += chinese_list[num]
return result
print(four_to_handstr('1000'))
print(four_to_handstr('1100'))
print(four_to_handstr('1110'))
print(four_to_handstr('1001'))
print(four_to_handstr('1010'))
print(four_to_handstr('1101'))
def jiaofen_to_handstr(jiaofen_str):
result = ""
num_len = len(jiaofen_str)
for i in range(num_len):
num = int(jiaofen_str[i])
result += chinese_list[num] + decimal_list[i]
return result
print(jiaofen_to_handstr('05'))
print(jiaofen_to_handstr('36'))
def integer_to_str(num_str, jiaofen_str):
str_len = len(num_str)
if str_len > 12:
print("数字太大,翻译不了,请重新输入!")
elif str_len > 8:
return four_to_handstr(num_str[:-8]) + '亿' \
+ four_to_handstr(num_str[-8:-4]) + '万' \
+ four_to_handstr(num_str[-4:]) + '元' + jiaofen_to_handstr(jiaofen_str)
elif str_len > 4:
return four_to_handstr(num_str[:-4]) + '万' \
+ four_to_handstr(num_str[-4:]) + '元' + jiaofen_to_handstr(jiaofen_str)
else:
return four_to_handstr(num_str) + '元' + jiaofen_to_handstr(jiaofen_str)
integer, fraction = divide_str(130020010025.236)
print(integer_to_str(integer, fraction))
integer, fraction = divide_str(12500010023.236)
print(integer_to_str(integer, fraction))
integer, fraction = divide_str(10023200.236)
print(integer_to_str(integer, fraction))