美文网首页
Python 数字转财务大写数字

Python 数字转财务大写数字

作者: 大象同学 | 来源:发表于2018-08-24 16:37 被阅读189次

    其实这应该是个挺简单的一小段代码,我却反复弄了很长时间。写这篇帖子把自己的思路理一理,希望以后遇到问题的时候能够形成自己的套路来迅速解决问题。

    首先,我觉得这个逻辑的框架是输入一个数字,然后转化成财务大写数字,可以先写个简单的架构。

    while True:
        num = input("请输入数字: ")
        result = ''
        if num.isdigit():
            result = convert(num)
            print(result)
    
        #回车退出
        elif num == '':
            break
        elif not num.isdigit():
            print("数字不合法请重新输入: ")
    

    基本框架有了,然后就是了解转换的基本规则,通过查找资料和自己整理,我得出了一下几条规则:

    1. 汉语读法是按照四位数字一组来划分的,5-8位的读法和1-4位相同,只要在后面加上就行了,9-12位加上亿
    2. 对于四位的数字,要对应在后面加上 ,个位后面不加;
    3. 开头和末尾的零要省略;
    4. 中间多个零的情况要合并成一个零。

    有了这些规则以后,还要做一点准备工作。

    unit = ['', '拾', '佰', '千']
    sep = ['', '万', '亿', '兆']
    accDigits = list('零壹贰叁肆伍陆柒捌玖')
    digits = list('0123456789')
    acc_dict = dict(zip(digits, accDigits))
    

    下面我们要做的是将数字分成4位一组的list。

    def four_digit_block(num):
        # fill zeros to make 4-digit blocks
        zero_fill_counter = len(num) % 4
        num = (
            '0' * (4 - zero_fill_counter) + num) if zero_fill_counter != 0 else num
    
        #split number to 4-digit blocks
        block_num = len(num) // 4
        block_list = [num[4 * i:4 * (i + 1)] for i in range(block_num)]
    
        return block_list
    

    切成4位一组的list以后,我们就可以开始转换和处理了。

    def convert(block_list, dictionary=acc_dict):
    #设置一个默认的字典参数是为了以后扩展的方便,如果想转换成`三千六百四十八`这样的形式,只要再添加个字典就可以了。
        result = ''
        for blockIndex in range(len(block_list)):
            block = block_list[blockIndex]
            for index in range(len(block)):
                #转换成大写财务数字,如果最大是十位那壹就省略
                if not (index == 2 and block[index] == '1' and blockIndex == 0):
                    result += dictionary[block[index]]
                #加上单位‘千佰拾等’
                if block[index] != '0':
                    result += unit[3 - index]
            #把多个连续的零合并成一个
            result = re.sub(r'(零+)', r'零', result)
            #去除两头的零
            result = result.strip('零')
            #加上4位分隔单位‘万,亿’等
            result += sep[len(block_list) - blockIndex - 1]
    
        return result
    

    最后把这几步拼起来就是成品了。

    import re
    unit = ['', '拾', '佰', '千']
    sep = ['', '万', '亿', '兆']
    accDigits = list('零壹贰叁肆伍陆柒捌玖')
    chDigits = list('零一二三四五六七八九')
    digits = list('0123456789')
    acc_dict = dict(zip(digits, accDigits))
    chn_dict = dict(zip(digits, chDigits))
    
    
    def four_digit_block(num):
        # fill zeros to make 4-digit blocks
        zero_fill_counter = len(num) % 4
        num = (
            '0' * (4 - zero_fill_counter) + num) if zero_fill_counter != 0 else num
    
        #split input number to 4-digit blocks
        block_num = len(num) // 4
        block_list = [num[4 * i:4 * (i + 1)] for i in range(block_num)]
    
        return block_list
    
    def convert(num, dictionary=acc_dict):
        block_list = four_digit_block(num)
        result = ''
        for blockIndex in range(len(block_list)):
            block = block_list[blockIndex]
            for index in range(len(block)):
                #转换成大写财务数字,如果最大是十位那壹就省略
                if not (index == 2 and block[index] == '1' and blockIndex == 0):
                    result += dictionary[block[index]]
                #加上单位‘千佰拾’等
                if block[index] != '0':
                    result += unit[3 - index]
            #把多个连续的零合并成一个
            result = re.sub(r'(零+)', r'零', result)
            #去除两头的零
            result = result.strip('零')
            #加上4位分隔单位‘万,亿’等
            result += sep[len(block_list) - blockIndex - 1]
    
        return result
    
    
    while True:
        num = input("请输入数字: ")
        result = ''
        if num.isdigit():
            result = convert(num)
            print(result)
    
        #回车退出
        elif num == '':
            break
        elif not num.isdigit():
            print("数字不合法请重新输入: ")
    

    最后的结果是这样的:

    请输入数字: 105000
    拾万伍千
    请输入数字: 200000
    贰拾万
    请输入数字: 215000
    贰拾壹万伍千
    请输入数字: 210500
    贰拾壹万零伍佰
    请输入数字: 10
    拾
    请输入数字: 11
    拾壹
    请输入数字: 120001
    拾贰万零壹
    请输入数字: 
    

    只做了整数部分,稍微再写一点就可以实现小数部分的功能了。

    相关文章

      网友评论

          本文标题:Python 数字转财务大写数字

          本文链接:https://www.haomeiwen.com/subject/uuobiftx.html