`
f002489
  • 浏览: 271654 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

人民币小写转大写程序,支持到万亿

阅读更多
# -*- coding: gbk -*-
#-------------------------------------------------------------------------------
# Name:        rmb.py
# Purpose:     人名币小写转大写,整数部分处理到万亿,小数部分只处理2位   
# Author:      pushad
#     
# Created:     28-11-2009
# Copyright:   (c) baby 2009
# Licence:     <your licence>
#-------------------------------------------------------------------------------
#!/usr/bin/env python
Number = ["零","壹","贰","叁","肆","伍","陆","柒","捌","玖"]
Other  = ["元","角","分","整"]
zi     = ["拾","佰","千","万","亿"]
dw     = ["元","万","亿"]
def main(inum):
    rmb = int(inum)
    xs  = round(inum - int(inum),2)
    rmbc = []
    #处理小数部分只处理到2位小数,不进行四舍五入
    xs = int(xs * 100)
    if xs != 0:
        if xs == 10:
            rmbc.append(Number[xs/10] + Other[1])
        elif xs < 10:
            rmbc.append(Number[xs] + Other[2])
        elif xs > 10:
            if xs%10:
                rmbc.append(Number[xs%10] + Other[2])
            rmbc.append(Number[xs/10] + Other[1])

    #无小数末尾添加‘整’,有小数添加小数描述
    if xs > 0:
        rmbc.append(Number[0])
    else:
        rmbc.append(Other[3])
   
    #处理整数部分
   
    #数字位数
    bit = 1
   
    #零标志位 用以添加 '0'
    zf = 0
   
    #万位标志
    bitw = 0
    #亿位标志
    bity = 0
   
    while rmb:
        if bit % 5 == 0:
            bitw = 0
        if bit % 9 == 0:
            bity = 0
        if rmb%10:           
            #亿位处理 大于9位,遇到第一个非0数字添加'亿'
            if bit >= 9 and bity == 0:
                bity = 1
                rmbc.append(dw[2])      
               
             #万处位理万 大于5位,遇到第一个非0数字添加'万',(9到12位之间单位为'亿')
            if bit >= 5 and (not 9 <= bit <= 12)  and bitw == 0:
                bitw = 1
                rmbc.append(dw[1])                    
               
            if (bit - 1) % 4 == 0:#个位
                rmbc.append(Number[rmb%10])               
            elif (bit - 2) % 4 == 0:#十位
                rmbc.append(Number[rmb%10] + zi[0])
            elif (bit - 3) % 4 == 0:#百位
                rmbc.append(Number[rmb%10] + zi[1])
            elif (bit - 4) % 4 == 0:#千位
                rmbc.append(Number[rmb%10] + zi[2])          
            zf = 0
        else:
            #当前位为0,如果上一位不为0,则添加'零'
            if    bit != 1 and zf == 0:
                rmbc.append(Number[0])
            zf = 1
        #从低位开始取数
        rmb = rmb / 10
        bit = bit + 1
    #处理后翻转列表
    rmbc.reverse()
    return rmbc

if __name__ == '__main__':
    #100,0000,0000
    test = [10,100,1000,10000,1010]
    test2 = [10001000.1,10010000.01,10200000,10200200,10000001]
    test3 = [10000,100000,110000,1000000,1010000,1100000,1110000,10000000,10010000,10100000,11000000]
    test4 = [10000,100000,110000,1000000,1010000,1100000,1110000,23123000360234]
    test5 = [10000100010000.12,11111111.10,1000000000,10000000000,10100000000,23123000360234,23000000000000,1000100000000]

    for num in test2:
        rmbc =  main(num)
        str = ''
        for i in rmbc:
            str = str + i
        print num,":",str.decode('gbk')
分享到:
评论
Global site tag (gtag.js) - Google Analytics