#数字转人民币汉字大写
def divide(num):
integer = int(num)
fraction = round((num - integer) * 100)
print("integer是:" +str(integer), "fraction是:" + str(fraction))
return (str(integer), str(fraction))
'''
#快速将字符串转成列表,省了敲引号了。
a1 = '零壹贰叁肆伍陆柒捌玖'
a2 = [a1[i] for i in range(len(a1))]
a2
['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
a1.split()也有类似效果
'''
han_list = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
unit_list = ['拾', '佰', '仟']
money_unit = ['角', '分']
def four_to_hanstr(num_str):
#print("num_str是:"+ 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 += han_list[num] + unit_list[num_len -2 -i]
else:
result += han_list[num]
#print("result是:" + result)
return result
def integer_to_str(num_str):
str_len = len(num_str)
if str_len >12:
print("数字太大,翻译不了!")
return
elif str_len >8:
return four_to_hanstr(num_str[:8])+"亿" +\
four_to_hanstr(num_str[-8:-4]) + "万" +\
four_to_hanstr(num_str[-4:])
elif str_len >4:
return four_to_hanstr(num_str[:-4]) + "万" +\
four_to_hanstr(num_str[-4:])
else:
return four_to_hanstr(num_str)
#小数部分转成汉字
def fraction_to_str(fraction):
if int(fraction) == 0:
result = ""
elif len(fraction)==2:
a = int(fraction[-2])
b = int(fraction[-1])
result = han_list[a] + "角" + han_list[b] + "分"
elif len(fraction) ==1:
b = int(fraction[-1])
result = "零" +han_list[b] + "分" #此处直接把“零角”写作“零几分”
else:
pass
return result
num = float(input("请输入一个浮点数: "))
integer, fraction = divide(num)
result_f = integer_to_str(integer) + "元" + fraction_to_str(fraction)
#删除多余的零
'''
for i in range(len(result_f )):
result_f = result_f.replace( "零分", "")
result_f = result_f.replace( "零元", "元")
result_f = result_f.replace( "零拾", "拾")
result_f = result_f.replace( "零佰", "佰")
result_f = result_f.replace( "零仟", "仟")
result_f = result_f.replace( "零万", "万")
result_f = result_f.replace( "零亿", "亿")
result_f = result_f.replace( "零零", "零")
result_f = result_f.replace( "零整", "")
'''
for i in range(len(result_f)):
result_f = result_f.replace( "零分", "")
result_f = result_f.replace( "零整", "")
units1 = '元拾佰仟万亿零'
for u in range(len(units1)):
result_f = result_f.replace( "零" + units1[u], units1[u])
if result_f[-1] != '分':
result_f = result_f + "整"
print(result_f)