所有分类
  • 所有分类
  • WordPress主题
  • WordPress插件
  • 发卡中心
  • 建站插件源码
  • 建站模版主题
  • 微信小程序
  • 手机Apps/Mobile
  • 游戏娱乐
  • 源码百宝箱
  • 站长工具箱
  • 知识分享

小写金额转大写金额Python代码

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("你输入的不是数字,请重新输入")

原文链接:https://c.larjie.com/amount-python/,转载请注明出处。
0

站点公告

本站SVIP会员,限时优惠!无限下载、全部功能、专属客服、资源订制、悬赏任务、专享资源... 随着资源不断增多,每90天提价10%!升级VIP 免责声明 赚钱赚积分
显示验证码
没有账号?注册  忘记密码?