模組:NumbersWithoutPrefixes

文出維基大典

此程序也,易阿剌伯數爲漢字,且不置詞綴。例:{{#invoke:NumbersWithoutPrefixes|num_to_chinese|1235}}成一二三五,{{#invoke:NumbersWithoutPrefixes|num_to_chinese|765}}成七六五。

如需詞綴,宜用Module:Numbers


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
}