Module:Roman

From Heroes of Might and Magic: Olden Era Official Wiki

Documentation for this module may be created at Module:Roman/doc

local p = {}

function p.main(frame)
    local input
    if type(frame) == 'table' and type(frame.args) == 'table' then
        local args = frame.args[1] and frame.args or frame:getParent().args
        input = args[1]
    else
        input = frame
    end

    local n = tonumber(input)
    if not n or n < 1 or n > 3999 then
        return 'N/A'
    end

    local vals = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 }
    local symbols = { 'M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I' }
    local result  = ''
    for i, v in ipairs(vals) do
        while n >= v do
            result = result .. symbols[i]
            n = n - v
        end
    end

    return result
end

return p