Fork me on GitHub

阿拉伯数字转中文数字

推荐阅读:

例如:将102转为一百零二;将08转为八。

local chnNumChar = {"零","一","二","三","四","五","六","七","八","九"} 
local chnUnitChar = {"","十","百","千"}
local chnUnitSection = {"","万","亿","万亿"}
local function sectionTOChinese(section,chineseNum)
    local setionChinese = ""
    local unitPos = 1
    local zero = true
    while(section>0)
    do
        local v = section%10
        if v == 0 then
            if (not zero) then 
                zero = true
                chineseNum = chnNumChar[1] .. chineseNum
            end
        else
            zero = false
            setionChinese = chnNumChar[v+1]
            setionChinese = setionChinese .. chnUnitChar[unitPos] 
            chineseNum = setionChinese .. chineseNum
        end 
        unitPos = unitPos + 1
        section = math.floor(section/10)
    end
      
    return chineseNum 
end

local function NumberToChinese(num)
    if num == 0 then  
        return "零" 
    end
    local unitPos = 1 --节权位标识  
    local All = ""
    local chineseNum = ""
    local needZero = false --下一小结是否需要补零  
    local strIns = ""
    while(num>0)
    do
        local section = num%10000--取最后面的那一个小节  
        if needZero then --判断上一小节千位是否为零,为零就要加上零  
            All = chnNumChar[1] .. All
        end
        chineseNum = sectionTOChinese(section,chineseNum)
        if section ~= 0 then
            strIns = chnUnitSection[unitPos]
            chineseNum = chineseNum .. strIns
        else 
            strIns = chnUnitSection[1]
            chineseNum = strIns .. chineseNum 
        end
        All = chineseNum .. All 
        chineseNum = "" 
        needZero = (section<1000) and (section>0)  
        num = math.floor(num/10000)
        unitPos = unitPos + 1
    end 
    return All
end
posted @ 2020-01-03 10:29  爱上游戏开发  阅读(...)  评论(...编辑  收藏
 >>>转载请注明出处