數字轉中文大寫=> 1234=> 一千二百三十四

# -*- coding: utf-8 -*-
# 最大值:九兆九千九百九十九億九千九百九十九萬九千九百九十九
import re
p = ['', '', '', '', '', '', '', '', '', '', '', '', '']
s = ['', '', '', '', '', '', '', '', '', '']
def num2zh(num_str):
    res = ''
    t = list(str(num_str))
    t.reverse()
    for idx, tmp in enumerate(t):
        if int(tmp) == 0:
            res = '' + res
            if idx % 4 == 0 and idx > 0:
                res = p[idx] + res
            continue
        res = s[int(tmp)] + p[idx] + res
    # 零+ -> 零
    out = re.sub(r'(\xe9\x9b\xb6)+', '', res)
    # 零萬 -> 萬
    out = re.sub(r'\xe9\x9b\xb6\xe4\xb8\x87', '', out)
    # 一十 -> 十
    out = re.sub(r'(^\xe4\xb8\x80\xe5\x8d\x81)', '', out)
    # 零億 -> 億 
    out = re.sub(r'\xe9\x9b\xb6\xe4\xba\xbf', '', out)
    # 億萬 -> 億 
    out = re.sub(r'\xe4\xba\xbf\xe4\xb8\x87', '', out)
    # 兆億 -> 兆 
    out = re.sub(r'\xe5\x85\x86\xe4\xba\xbf', '', out)
    # 去掉最後的零
    out = re.sub(r'(\xe9\x9b\xb6)$', '', out)
    return out
print(num2zh(1234))
相關文章
相關標籤/搜索