置顶公告:【置顶】关于临时开启评论区所有功能的公告(2022.10.22) | 【置顶】关于本站Widget恢复使用的公告
  • 你好~!欢迎来到萌娘百科镜像站!如需查看或编辑,请联系本站管理员注册账号。
  • 本镜像站和其他萌娘百科的镜像站无关,请注意分别。

Module:Zhnum

贴贴♀百科,万娘皆可贴的百科全书!转载请标注来源页面的网页链接,并声明引自贴贴百科。内容不可商用。
跳到导航 跳到搜索
Template-info.svg 模块文档  [创建] [刷新]
-- Originally designed and coded by Maya (U:MJH)

local getArgs = require("Module:Arguments").getArgs

local levels = {'', '万', '亿', '兆', '京', '垓', '秭', '穰', '沟', '涧', '正', '载'}

local function zhnum1(number)
    local hanzi = {
    	[0] = '零', '一', '二', '三', '四', '五', '六', '七', '八', '九'
    }
    number = tonumber(number)
    if hanzi[number] then
    	return hanzi[number]
    end
    error('zhnum1错误:输入无效')
end

local function zhnum4(number, isFollow)
	if number == nil or number == '' then
		return ''
	end
    number = tonumber(number)
    if not number or number < 0 or number > 9999 or number ~= math.floor(number) then
        error('zhnum4错误:输入无效')
    end
    
    local isSeparated = false
    local retstr = ''
    local digit = 0
    if number > 999 then
        digit = math.floor(number / 1000)
        retstr = retstr .. zhnum1(digit) .. '千'
        isFollow = true
    else
        isSeparated = true
    end
    number = number % 1000

    if number > 99 then
        if isFollow and isSeparated then
            retstr = retstr .. '零'
        end
        digit = math.floor(number / 100)
        retstr = retstr .. zhnum1(digit) .. '百'
        isFollow = true
        isSeparated = false
    else
        isSeparated = true
    end
    number = number % 100

    if number > 9 then
        if isFollow and isSeparated then
            retstr = retstr .. '零'
        end
        digit = math.floor(number / 10)
        local decaPrefix = zhnum1(digit)
        if not isFollow and digit == 1 then
            decaPrefix = ''
        end
        retstr = retstr .. decaPrefix .. '十'
        isFollow = true
        isSeparated = false
    else
        isSeparated = true
    end
    number = number % 10

    if number > 0 then
        if isFollow and isSeparated then
            retstr = retstr .. '零'
        end
        digit = math.floor(number)
        retstr = retstr .. zhnum1(digit)
        isFollow = true
        isSeparated = false
    else
        isSeparated = true
    end
    
    if not isFollow and retstr == '' then
        return '零'
    else
        return retstr
    end
end

local function zhnum48(number)
    local numbernum = tonumber(number)
    local numberstr = tostring(number)
    local retstr = ''
    if numbernum < 0 then
         retstr = '负'
    end

    local zhnumstr = ''
    local level = 1
    local prefix = ''
    local midfix = ''
    while numbernum > 9999 or numbernum < -9999 do
        subnumberstr = string.sub(numberstr, -4, -1)
        numberstr = string.sub(numberstr, 1, -5)
        prefix = zhnum4(subnumberstr, true)
        if prefix == '' then midfix = '' else midfix = levels[level] end
        zhnumstr = prefix .. midfix .. zhnumstr
        level = level + 1
        numbernum = tonumber(numberstr)
    end
    if numbernum < 0 then numbernum = -numbernum end
    zhnumstr = zhnum4(numbernum, false) .. levels[level] .. zhnumstr
    return retstr .. zhnumstr
end

local zhnum = {
    zhnum1 = zhnum1,
    zhnum1f = function(frame)
        local args = getArgs(frame)
        return zhnum1(args[1])
    end,
    zhnum4 = zhnum4,
    zhnum4f = function(frame)
        local args = getArgs(frame)
        return zhnum4(args[1], args["follow"] == "true")
    end,
    zhnum48 = zhnum48,
    zhnum48f = function(frame)
        local args = getArgs(frame)
        return zhnum48(args[1])
    end
}

return zhnum