Jump to content

Module:Ordinal

Permanently protected module
From Wikipedia, the free encyclopedia

--[[ This template will add the appropriate ordinal suffix to a given integer.Please do not modify this code without applying the changes first atModule:Ordinal/sandbox and testing.]]localp={}localyesno=require('Module:Yesno')-- boolean value interpretation--[[This function converts an integer value into a numeral followed by ordinal indicator.The output string might contain HTML tags.Usage:{{#invoke:Ordinal|ordinal|1=|2=|sup=}}{{#invoke:Ordinal|ordinal}} - uses the caller's parametersParameters 1: Any number or string. 2: Set to "d" if the module should display "d" instead of "nd" and "rd". sup: Set to yes/no to toggle superscript ordinal suffix.]]functionp.ordinal(frame)localargs=frame.argsifargs[1]==nilthenargs=frame:getParent().argsendifargs[1]==nilthenargs[1]="{{{1}}}"endreturnp._ordinal(args[1],(args[2]=='d'),yesno(args.sup))endfunctionp._ordinal(n,d,sup)localx=tonumber(mw.ustring.match(n,"(%d*)%W*$"))localsuffix="th"-- If tonumber(n) worked:ifxthenlocalmod10=math.abs(x)%10localmod100=math.abs(x)%100ifmod10==1andmod100~=11thensuffix="st"elseifmod10==2andmod100~=12thenifdthensuffix="d"elsesuffix="nd"endelseifmod10==3andmod100~=13thenifdthensuffix="d"elsesuffix="rd"endendendifsupthensuffix="<sup>"..suffix.."</sup>"endreturnn..suffixendreturnp
close