1. 首页
  2. > 代理记账 >

python将财务数字转化成汉字大写(python将小写字母转换成大写)




小伙伴们应该收到过银行账单,账单上的金额可能会写成金额大写的形式,比如“您本月的信用卡账单陆万叁仟元整”,那么肯定会需要一个方法将数字转换成这种形式。


规则不难,但是用代码实现的时候还是会有点小坑,不信的话可以先自己写的试试。


壹贰叁肆伍陆柒捌玖拾佰仟万亿壹贰叁肆伍陆柒捌玖拾佰仟万亿壹贰叁肆伍陆柒捌玖拾佰仟万亿壹贰叁肆伍陆柒捌玖拾佰仟万亿壹贰叁肆伍陆柒捌玖拾佰仟万亿壹贰叁肆伍陆柒捌玖拾佰仟万亿壹贰叁肆伍陆柒捌玖拾佰仟万亿



#encoding=utf-8 class Num2Chinese: def __init__(self): self.ch = [零,壹,贰,叁,肆,伍,陆,柒,捌,玖] self.unit0 = [仟,佰,拾,] self.unit1 = [亿,万,] # 不大于10000的数 def convertFor0000(self,num): tmp = (0000 str(num))[-4:] if tmp==0000: return 零 else: tmpCh = for i in range(4): tmpCh = self.ch[int(tmp[i])] self.unit0[i] tmpCh = tmpCh.replace("零仟","零").replace("零佰","零").replace("零拾","零") tmpCh = tmpCh.replace("零零零零","零").replace("零零零", "零").replace("零零", "零") if tmpCh.endswith(零): tmpCh = tmpCh[0:-1] return tmpCh def convert(self,num): if num>0 and num <= 999999999999: tmp = (000000000000 str(num))[-12:] tmpCh = for i in range(0, 12, 4): numStr = tmp[i:i 4] tmpCh = self.convertFor0000(numStr) self.unit1[i // 4] tmpCh = tmpCh.replace("零亿", "零").replace("零万", "零") tmpCh = tmpCh.replace("零零零零", "零").replace("零零零", "零").replace("零零", "零") if tmpCh.startswith(零): tmpCh = tmpCh[1:] if tmpCh.endswith(零): tmpCh = tmpCh[0:-1] # if tmpCh.startswith(一十): # tmpCh = tmpCh[1:] return tmpCh elif num<0 and num >= -999999999999: return 负 self.convert(0-num) elif num == 0: return 零 elif num>999999999999 or num<-999999999999: return 超出范围 else: return 未知 if __name__==__main__: print(Num2Chinese().convert(100108500010))

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至123456@qq.com 举报,一经查实,本站将立刻删除。

联系我们

工作日:9:30-18:30,节假日休息