-

本WIKI编辑权限开放,欢迎收藏起来防止迷路,也希望有爱的小伙伴和我们一起编辑哟~ 编辑帮助:目录BWIKI反馈留言板


请选择语言:

版本250722.2
全站通知:

模块:数据补零

来自赛尔号WIKI_BWIKI_哔哩哔哩
跳到导航 跳到搜索

此模块的文档可以在模块:数据补零/doc创建

-- 给数字或字符串左边补 0 直到指定长度
-- 用法:{{#invoke:PadZero|main|123|6}} → 000123
local p = {}

-- 主入口
function p.main(frame)
    -- 取参数
    local str   = frame.args[1] or frame.args['s'] or ''
    local width = tonumber(frame.args[2] or frame.args['w']) or 0

    -- 去掉首尾空格
    str = tostring(str):match('^%s*(.-)%s*$')

    -- 如果已经是目标长度或更长,直接返回
    if #str >= width then
        return str
    end

    -- 计算需要补的 0 个数
    local zeros = string.rep('0', width - #str)
    return zeros .. str
end

return p