Module:RomanNum
From Historical Hastings
Documentation for this module may be created at Module:RomanNum/doc
local getArgs = require('Module:Arguments').getArgs local p = {} local function _toArabic(roman) -- strip non-roman numerals and convert to uppercase for parsing roman = string.gsub(roman, '[^IVXLCDMivxlcdm]', ''):upper() local numHash = { ["M"] = 1000, ["D"] = 500, ["C"] = 100, ["L"] = 50, ["X"] = 10, ["V"] = 5, ["I"] = 1 } local total = 0 local i = 1 local strlen = roman:len() -- get last char separately; if i >= strlen, this loop's i+1 is out of bounds while i < strlen do local thisChar = numHash[string.sub(roman, i, i)] local nextChar = numHash[string.sub(roman, i + 1, i + 1)] -- e.g. IX is 10 minus 1, and XL is 50 minus 10 if thisChar < nextChar then total = total + ( nextChar - thisChar ) i = i + 2 -- consumed 2 (this + next) else total = total + thisChar i = i + 1 -- consumed 1 end end -- leftover from i, i+1 loop above if i <= strlen then total = total + numHash[string.sub(roman, i, i)] end return total end function p.main(frame) local arguments = getArgs(frame, {trim = true, removeBlanks = true}) local numeral = arguments[1] if numeral == nil then return end if arguments.toarabic or arguments[2] == 'toarabic' then return _toArabic(numeral) else return 'No Mode Given' end end return p