python 小写金额转大写金额

摘自:http://shine-it.net/index.php?topic=14575.0

复制代码
def _rmb_upper(value):
        """
        人民币大写
        来自:http://topic.csdn.net/u/20091129/20/b778a93d-9f8f-4829-9297-d05b08a23f80.html
        传入浮点类型的值返回 unicode 字符串
        """
        map  = [u"",u"",u"",u"",u"",u"",u"",u"",u"",u""]
        unit = [u"",u"",u"",u"",u"",u"",u"",u"",u"",u"",u"亿",
                u"",u"",u"",u"",u"",u"",u"",u""]

        nums = []   #取出每一位数字,整数用字符方式转换避大数出现误差   
        for i in range(len(unit)-3, -3, -1):
            if value >= 10**i or i < 1:
                nums.append(int(round(value/(10**i),2))%10)

        words = []
        zflag = 0   #标记连续0次数,以删除万字,或适时插入零字
        start = len(nums)-3     
        for i in range(start, -3, -1):   #使i对应实际位数,负数为角分
            if 0 != nums[start-i] or len(words) == 0:
                if zflag:
                    words.append(map[0])
                    zflag = 0
                words.append(map[nums[start-i]])
                words.append(unit[i+2])
            elif 0 == i or (0 == i%4 and zflag < 3): #控制‘万/元’
                words.append(unit[i+2])
                zflag = 0
            else:
                zflag += 1
                
        if words[-1] != unit[0]:    #结尾非‘分’补整字
            words.append(u"")
        return ''.join(words)
复制代码

留言

熱門文章