|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 1809228982 于 2020-5-22 16:36 编辑
代码单位过拾万的都有点乱了,过亿的要咋弄呢?有帮优化的吗
import os #清屏使用
unit = ['万', '仟', '佰', '拾', '']
rmb = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
rmb_point = ['角', '分']
while True:
number = input("请输入金额(输入Q退出):")
if number == 'Q' or number == 'q':
break;
try:
float(number)
except ValueError as exc:
print("\n请输入正确的数字!\n")
else:
point_flag = False #初始化小数点
#检查是否含有小数点
if '.' in number:
(number, point_right) = number.split('.')
point_flag = True
point_right_len = len(point_right)
if len(number) > 8:
os.system('cls')
print("\t\t-----------------------------------")
print("\t\t 错误!暂不支持过亿的单位换算!")
print("\t\t-----------------------------------")
print('\n\n\n\n\n\n\n')
exit()
zero_flag = True #判断是否去除多余的零
rmb_upper = '大写:'
lenght = len(number)#初始化数字(小数点左边的数字)的长度
wan_lenght = 0 #初始化万单位的数字长度
unit_flag = 5 - lenght #初始化单位
begin = 0 #初始化开始的位置
#检查数字是否超过万单位,如果查过则重新为各项数据赋值
wan_flag = False
if lenght > 5:
wan_lenght = 9 - lenght
unit_flag = 1
wan_flag = True
begin = lenght - 4
#进行超万单位的转换
if wan_flag:
for i in range(5 - wan_lenght):
if number[i] == '0':
zero_flag = False
else:
if not zero_flag:
rmb_upper += rmb[0]
zero_flag = True
rmb_upper += rmb[int(number[i])] + unit[wan_lenght]
wan_lenght += 1
rmb_upper += '万'
#进行万及以下单位的转换
for i in range(begin, lenght):
if number[i] == '0':
zero_flag = False
else:
if not zero_flag:
rmb_upper += rmb[0]
zero_flag = True
rmb_upper += rmb[int(number[i])] + unit[unit_flag]
unit_flag += 1
#转换小数点右边的数字
if not point_flag or not point_right:
rmb_upper += '元整'
else:
if point_right[0] != '0':
if point_right_len == 1:
rmb_upper += '元' + rmb[int(point_right[0])] + rmb_point[0]
else:
rmb_upper += '元' + rmb[int(point_right[0])] + rmb_point[0] + rmb[int(point_right[1])] + rmb_point[1]
else:
if point_right_len == 1 or (point_right_len ==2 and point_right[1] == '0'):
rmb_upper += '元整'
else:
rmb_upper += '元' + rmb[0] + rmb[int(point_right[1])] + rmb_point[1]
#输出
for i in range(2):
print()
print(rmb_upper)
for i in range(3):
print()
本帖最后由 sunrise085 于 2020-5-22 17:33 编辑
这个是我之前写的金额转换。可以高达万亿 numch = ["零","壹","贰","叁","肆","伍","陆","柒","捌","玖"]
rmbqian = ["","拾","佰","仟"]
rmbpian = ["","萬","亿","萬"]
#10000以内整数进行转换
def qian_exc(str1):
num=int(str1)
result=''
k=0
while num:
a=num%10
num=num//10
if a!=0:
result=rmbqian[k]+result
result=numch[a]+result
k+=1
result=result.strip('零')
if result.count('零')>1:
result=result.replace('零','',1)
if int(str1)<1000:
result='零'+result
return result
#将整数部分进行转换
def yuan_exc(str_yuan):
yuan=[]
while str_yuan:
yuan.append(str_yuan[-4:])
str_yuan=str_yuan[:-4]
readwords='圆'
for i in range(len(yuan)):
piece=qian_exc(yuan[i])
if piece!='零':
readwords = rmbpian[i] + readwords
readwords = piece + readwords
if readwords[0]=='零':
readwords=readwords[1:]
readwords= readwords.replace('零零','零')
if readwords[-2]=='零':
readwords=readwords[:-2]+'圆'
return readwords
#将小数部分进行转换
def fen_exc(fen):
fen_words=''
if fen!=0: #不是零分的时候进行转换
fen_p=fen%10
fen_b=fen//10
if fen_p!=0:
fen_words=numch[fen_p]+'分'
if fen_b!=0:
fen_words=numch[fen_b]+'角'+fen_words
return fen_words
fen=0
fen_words=''
yuan_words=''
l=1
str1=input('请输入:')
while l: #保证输入正确
l=0
try:
f=float(str1)
if f<0:
str1=input('请输入一个正数:')
l=1
except:
str1=input('有错误字符,请重新输入:')
temp=str1.split('.') #以小数点切片,分为整数部分和小数部分
if len(temp)==2:
l=len(temp[1]) #小数部分长度,若多于两位会进行四舍五入
if l>0:
fen=round(int(temp[1])/(10**(l-2))) #小数部分四舍五入到100以内
if fen==100: #小数部分转换后为100分,需要进位,整数部分加一
hudr=True
temp[0]=str(int(temp[0])+1)
else:
fen_words=fen_exc(fen)
if temp[0]!='0' and temp[0]!='': #整数部分不为0,进行转换
yuan_words=yuan_exc(temp[0])
say_words=yuan_words+fen_words #整数部分和小数部分进行拼接
if say_words=='': #输入为0时,say_words==''
say_words='零圆'
if fen_words=='': #若没有小数部分,在最后添加“整”
say_words=say_words+'整'
if l>2: #若进行过四舍五入,则进行说明
print('输入小数多于两位,四舍五入后约:'+say_words)
else:
print(say_words)
|
|