近日,小爬的工作中,需要将货币数字金额(如:123234.56)转化成汉字大写的形式。互联上看了一圈他人博客上的自定义函数,稍微都存有瑕疵,不符合汉语习惯:

1、比如,“六分”不能被转化为“零角六分”,“.00"转为了”零角零分“灯,实在有些拗口;

2、有的支持的位数不够多,只定义了一万以下的数字转化方法,这样的函数有它的局限性。

  小爬最后找到一个他人写的、效果比较理想的js函数,稍加修改,然后用python的语法重新实现了,可以很完美地实现上述金额转化为大写的要求,支持的数大小可以超过一亿,供参考!

def formatCurrency(currencyDigits):
    '''本函数旨在将数字化的金额(不含千分符)转化为中文的大写金额'''
    maximum_number=99999999999.99
    cn_zero=""
    cn_one=""
    cn_two=""
    cn_three=""
    cn_four=""
    cn_five=""
    cn_six=""
    cn_seven=""
    cn_eight=""
    cn_nine=""
    cn_ten=""
    cn_hundred=""
    cn_thousand=""
    cn_ten_thousand=""
    cn_hundred_million="亿"
    cn_symbol="人民币"
    cn_dollar=""
    cn_ten_cent=""
    cn_cent=""
    cn_integer=""
    integral=None
    decimal=None
    outputCharacters=None
    parts=None
    digits, radices, bigRadices, decimals=None,None,None,None
    zeroCount=None
    i, p, d=None,None,None
    quotient, modulus=None ,None
    currencyDigits=str(currencyDigits)
    if currencyDigits=="":
        return ""
    if float(currencyDigits)>maximum_number:
        print("转换金额过大!")
        return ""
    parts = currencyDigits.split(".")
    if len(parts)>1:
        integral = parts[0]
        decimal = parts[1]
        decimal=decimal[0:2]
        if decimal=="0" or decimal=="00":
            decimal=""
    else:
        integral=parts[0]
        decimal=""
    digits=[cn_zero,cn_one,cn_two,cn_three,cn_four,cn_five,cn_six,cn_seven,cn_eight,cn_nine]
    radices=["",cn_ten,cn_hundred,cn_thousand]
    bigRadices=["",cn_ten_thousand,cn_hundred_million]
    decimals=[cn_ten_cent,cn_cent]
    outputCharacters = ""
    if int(integral)>0:
        zeroCount = 0
        for i in range(len(integral)):
            p = len(integral) - i - 1
            d = integral[i]
            quotient = int(p / 4)
            modulus = p % 4
            if d=="0":
                zeroCount+=1
            else:
                if zeroCount>0:
                    outputCharacters += digits[0]
                zeroCount=0
                outputCharacters = outputCharacters+ digits[int(d)] + radices[modulus]
            if modulus==0 and zeroCount < 4:
                outputCharacters =outputCharacters + bigRadices[quotient]
        outputCharacters += cn_dollar
    if decimal!="":
        jiao = decimal[0]
        if jiao=="":
            jiao="0"
        try:
            fen = decimal[1]
        except:
            fen="0"
        if fen=="":
            fen="0"
        if jiao=="0" and fen=="0":
            pass
        if jiao=="0" and fen !="0":
            outputCharacters = outputCharacters + cn_zero + digits[int(fen)] + decimals[1]
        if jiao !="0" and fen=="0":
            outputCharacters =outputCharacters + digits[int(jiao)] + decimals[0]
        if jiao!="0" and fen !="0":
            outputCharacters =outputCharacters + digits[int(jiao)] + decimals[0]
            outputCharacters =outputCharacters + digits[int(fen)] + decimals[1]
    if outputCharacters == "":
        outputCharacters = cn_zero + cn_dollar
    if decimal=="":
        outputCharacters = outputCharacters+ cn_integer
    outputCharacters = outputCharacters
    return outputCharacters

  小爬随便输入几个较大的货币数字给这个函数,输出的结果非常理想:

1 for currency in [23104214618.134,23423424.22,20012.35,23456.00,104213421.10,1000043.01]:
2     capital_currency=formatCurrency(currency)
3     print(str(currency)+":\t"+capital_currency)

结果如下:

23104214618.134:        贰佰叁拾壹亿零肆佰贰拾壹万肆仟陆佰壹拾捌元壹角叁分
23423424.22:    贰仟叁佰肆拾贰万叁仟肆佰贰拾肆元贰角贰分
20012.35:       贰万零壹拾贰元叁角伍分
23456.0:        贰万叁仟肆佰伍拾陆元整
104213421.1:    壹亿零肆佰贰拾壹万叁仟肆佰贰拾壹元壹角
1000043.01:     壹佰万零肆拾叁元零壹分

  

02-14 04:12