python 小写金额 转 大写金额
小写金额转大写金额
转自https://www.52pojie.cn/thread-1658154-1-1.html
import re
dictionary = {'0': '零', '1': '壹', '2': '贰', '3': '叁', '4': '肆', '5': '伍', '6': '陆', '7': '柒', '8': '捌', '9': '玖'}
level1 = {1: '圆', 2: '万', 3: '亿', 4: '兆'}
level2 = {2: '拾', 3: '佰', 4: '仟'}
level3 = {1: '角', 2: '分'}
class DecimalToChn:
def __init__(self, que_decimal):
self.que_decimal = str(que_decimal)
self.num_float = None
self.num_integer = None
self.pre = []
self.integer_chn = []
self.float_chn = []
def query(self):
'''
验证数字,整数大于16位数,小数多余小数点后面2位数的全部弄成0
:return:
'''
if "." not in self.que_decimal:
self.que_decimal = self.que_decimal + "." + "0"
if len(self.que_decimal.split('.')) <= 16 and len(self.que_decimal.split('.')) <= 2:
return self.que_decimal
else:
return 0
def split_num(self):
"""
将进来的数字拆成整数部分跟小数部分
:return:
"""
number = self.query()
self.num_integer, self.num_float = number.split('.')
def process_integer(self):
"""
处理整数部分
:return:
"""
length = len(self.num_integer)
# 整数部分小于千位的
if length <= 4:
self.pre.append(self.num_integer)
# print("整数部分小于4位的:",self.pre)
else:
# 整数部分大于千位的
extra = length % 4
if extra != 0:
self.pre.append(self.num_integer[0: extra])
length = length - extra
start, end = extra, 4 + extra
else:
start, end = 0, 4
while length:
self.pre.append(self.num_integer[start: end])
start += 4
end += 4
length -= 4
# print("整数部分大于4位的:", self.pre)
def per_conver(self, num):
length = len(num)
flag = True
# Special
# deal with '0000'
if num == '0000':
return ''
# deal with '1000'
if num[1:] == '000':
return dictionary[num[0]] + level2[4]
# Normal
res, p = '', length
for i in range(0, length):
# deal with prefix is '0'
if (res == '' or res == dictionary[num[i]]) and num[i] == '0':
# only one '0'
if flag:
res += dictionary[num[i]]
flag = False
p -= 1
continue
# deal with '1001'
if i + 1 < length and num[i + 1] == '0' and num[i] == '0':
p -= 1
continue
# deal with '1010' '1110'
if i == length - 1 and num[i] == '0':
continue
res += dictionary[num[i]]
if p > 1 and num[i] != '0':
res += level2[p]
p -= 1
return res
def convert_integer(self):
self.split_num()
self.process_integer()
# print(self.pre)
p = len(self.pre)
for i in self.pre:
per_res = self.per_conver(i)
self.integer_chn += per_res
if p > 0 and per_res != '':
self.integer_chn += level1[p]
else:
self.integer_chn += level1[1]
p -= 1
def convert_float(self):
# print(self.num_float)
if self.num_float == "00":
self.num_float = ""
for i in range(0, len(self.num_float)):
self.float_chn += dictionary[self.num_float[i]]
if (i + 1) <= 2:
self.float_chn += level3[i + 1]
def convert(self):
self.convert_integer()
self.convert_float()
# print(self.float_chn)
if self.float_chn:
return ''.join(self.integer_chn + self.float_chn)+"整"
return ''.join(self.integer_chn) +"整"
def re_num(float_number):
value = re.compile(r'^[-+]?[0-9]+\.?[0-9]+$')
result = value.match(float_number)
return result
if __name__ == '__main__':
while True:
num = input("请输入您想要转的金额:")
result = re_num(num)
if result:
print(DecimalToChn(num).convert())
continue
print("你输入的不是数字,请重新输入")