This Lua module is used on 5,600+ pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them.
This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing.
This module implements the {{Roman}} template. For behavioral documentation, please see the template page. For test cases, please see Template:Roman/testcases.
Module supports 0 as a Roman numeral, displays as 'N'.
Prior to 25 April 2016, used to display 69105 as LXVMMMMCV. With the addition of IX and IV being 9000 and 4000 respectively, we now display 69105 as LXIXCV.
Module handles decimal, fractional, and arithmetic expressions to a precision of 1/1728. Uses #expr: at line 122.
Handling tricky cases (like 0.00001 and 99.99999)
Find the Roman numerals for the integer part of the number.
If the number is not an integer:
Add half of the smallest unit (1/1728) to simulate rounding instead of truncation.
Ensure this new result is between 1/1728 and 1727/1728. (actually 1.1/1728 and 1727.1/1728 due to floating point rounding issues)
Hence, 0.00001 is guaranteed to have at least the smallest unit symbol (instead of being blank or 0), and 99.99999 does not display as 100 or 99 and 2 halves.
-- This module implements {{Roman}}.require[[strict]]localp={}-- This function implements the {{overline}} template.localfunctionoverline(s)returnmw.ustring.format('<span style="text-decoration:overline;">%s</span>',s)end-- Gets the Roman numerals for a given numeral table. Returns both the string of-- numerals and the value of the number after it is finished being processed.localfunctiongetLetters(num,t)localret={}for_,vinipairs(t)dolocalval,letter=unpack(v)whilenum>=valdonum=num-valtable.insert(ret,letter)endendreturntable.concat(ret),numend-- The main control flow of the module.localfunction_main(args)-- Get input and exit displaying nothing if the input is empty.ifargs[1]==nilthenreturnendlocalnum=tonumber(args[1])ifnotnumornum<0ornum==math.hugethenerror('Invalid number '..args[1],2)elseifnum==0thenreturn'N'end-- Return a message for numbers too big to be expressed in Roman numerals.ifnum>=5000000thenreturnargs[2]or'N/A'endlocalret=''-- Find the Roman numerals for the large part of numbers.-- 23 April 2016 - tweaked to >= 4000 to accept big Roman 'IV'-- The if statement is not strictly necessary, but makes the algorithm -- more efficient for smaller numbers.ifnum>=4000thenlocalbigRomans={{1000000,'M'},{900000,'CM'},{500000,'D'},{400000,'CD'},{100000,'C'},{90000,'XC'},{50000,'L'},{40000,'XL'},{10000,'X'},{9000,'IX'},{5000,'V'},{4000,'IV'},}localbigLettersbigLetters,num=getLetters(num,bigRomans)ret=overline(bigLetters)end-- Find the Roman numerals for numbers less than the big Roman threshold.localsmallRomans={{1000,'M'},{900,'CM'},{500,'D'},{400,'CD'},{100,'C'},{90,'XC'},{50,'L'},{40,'XL'},{10,'X'},{9,'IX'},{5,'V'},{4,'IV'},{1,'I'}}localsmallLetters=getLetters(num,smallRomans)ret=ret..smallLettersifargs.fraction=='yes'then-- Find the Roman numerals for the fractional parts of numbers.-- If num is not a whole number, add half of 1/1728 (the smallest unit) to equate to rounding.-- Ensure we're not less than the smallest unit or larger than 1 - smallest unit-- to avoid getting two "half" symbols or no symbols at allnum=num-math.floor(num)ifnum~=0thennum=math.max(1.1/1728,math.min(1727.1/1728,num+1/3456))endlocalfractionalRomans={{1/2,'S'},{5/12,"''':'''•''':'''"},{1/3,"'''::'''"},{1/4,"''':'''•"},{1/6,"''':'''"},{1/12,'•'},{1/24,'Є'},{1/36,'ƧƧ'},{1/48,'Ɔ'},{1/72,'Ƨ'},{1/144,'<s>Ƨ</s>'},{1/288,'℈'},{1/1728,'»'},}localfractionalLetters=getLetters(num,fractionalRomans)ret=ret..fractionalLettersendreturnretendfunctionp.main(frame)-- If called via #invoke, use the args passed into the invoking-- template, or the args passed to #invoke if any exist. Otherwise-- assume args are being passed directly in from the debug console-- or from another Lua module.localorigArgsifframe==mw.getCurrentFrame()thenorigArgs=frame:getParent().argsfork,vinpairs(frame.args)doorigArgs=frame.argsbreakendelseorigArgs=frameend-- Trim whitespace and remove blank arguments.localargs={}fork,vinpairs(origArgs)doiftype(v)=='string'thenv=mw.text.trim(v)endifv~=''thenargs[k]=vendend-- exit if not given anythingifargs==nilorargs=={}thenreturnend-- Given mathematical expression, simplify to a numberiftype(args[1])=='string'thenlocalsuccess,result=pcall(mw.ext.ParserFunctions.expr,args[1])ifsuccessthenargs[1]=resultend-- else, pass to _main routine and try to let Lua's tonumber handle itendreturn_main(args)endreturnp