模組:Numbers

文出維基大典

此程序也,易阿剌伯數爲漢字。例:{{#invoke:Numbers|num_to_chinese|1235}}成一千二百三十五,{{#invoke:Wn/lzh/Numbers|num_to_chinese|765}}成七百六十五。

如需詞綴,宜用Module:NumbersWithoutPrefixes


local digits = { '一', '二', '三', '四', '五', '六', '七', '八', '九' }
 
local places = {
    {'億', 100000000}, 
    {'萬', 10000},
    {'千', 1000},
    {'百', 100},
    {'十', 10}
}

local ling = '零'

local function num_to_chinese_int(i)
    local result = ''
    local needs_ling = false
    local current_number =  i

    if i == 0 then
        return 'ling'
    end
    
    for k, place in pairs(places) do
        local current_place = math.floor(current_number / place[2])
        local rest = current_number % place[2]
        
        if current_place > 0 then
            if needs_ling then
                result = result .. ling
            end
            result = result .. num_to_chinese_int(current_place) .. place[1]
            
            needs_ling = false
        elseif result ~= '' then
            -- if we skipped some place, we need to add 零
            needs_ling = true
        end
        
        current_number = rest
    end
    
    if current_number > 0 and current_number < 10 then
        if needs_ling then
            result = result .. ling
        end
        
        result = result .. digits[current_number]
    end
    
    return result
end
 
return {
  num_to_chinese = function(frame)
      return num_to_chinese_int(tonumber(frame.args[1]))
  end
}