This Lua module is used on many 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 depends on the following other modules:
locallang=mw.language.getContentLanguage()localMath=require('Module:Math')localSortKey=require('Module:Sortkey')-- constantslocalINF=math.hugelocalNEGINF=-math.hugelocalMINUS='−'-- Unicode U+2212 MINUS SIGN (UTF-8: e2 88 92)---------------------------------------------------------------------------------- Nts class--------------------------------------------------------------------------------localNts={}Nts.__index=NtsNts.formats={no=true,yes=true,}functionNts.new(args)localself=setmetatable({},Nts)self:parseNumber(args[1])-- Set the format stringself.format=args.formator'yes'ifnotNts.formats[self.format]thenerror(string.format("'%s' is not a valid format",tostring(self.format)),0)end-- To display some text before the display version of the number-- {{nts|123456789.00123|prefix=approx. }} → approx. 123,456,789.00123self.prefix=args.prefixor''-- debug infoself.debug=args.debugor'no'self.quiet=args.quietor'no'returnselfend-- Parse the entered numberfunctionNts:parseNumber(s)-- sanitizes=sor'';s=string.gsub(s,'−','-')s=string.gsub(s,MINUS,'-')self.rawNumberString=s-- fractions. was somewhat but completely broken at some pointself.isFraction=(string.find(s,'/')~=nil)ifself.isFractionthenerror(string.format("Fractions are not supported",tostring(s)),0)end-- format detectionself.isScientificNotation=(string.find(s,'e')~=nil)-- parse with language optionsself.number=lang:parseFormattedNumber(s)-- parse with fallbackifnotself.numberthenself.number=tonumber(s)end-- allow for empty string as a valueifnotself.numberthen-- error(string.format(-- "'%s' is not a valid number",-- tostring(s)-- ), 0)self.number=NEGINFendifself.number<0thenself.sign=MINUSelseself.sign=''endself.absNumber=math.abs(self.number)ifself.absNumber~=INFthenself.magnitude=math.floor(math.log10(self.absNumber))self.significand=self.number/10^self.magnitudeself.precision=Math._precision(self.rawNumberString)self.integer=math.floor(self.absNumber)self.fractional=math.abs(self.number-self.integer)endendfunctionNts:makeDisplay()localret={}ifself.quiet=='yes'thenreturn''endret[#ret+1]=self.prefixlocalsciNotation=string.find(tostring(self.number),'e')ifself.absNnumber==INForisNaN(self.number)orself.magnitude==nilormath.abs(self.magnitude)==INFthenret[#ret+1]=string.gsub(self.rawNumberString,'-',MINUS)elseifsciNotation~=nilormath.abs(self.magnitude)>=9thenret[#ret+1]=self.signifself.format=='yes'thenret[#ret+1]=lang:formatNum(math.abs(self.number*10^-self.magnitude))elseret[#ret+1]=math.abs(self.number*10^-self.magnitude)endret[#ret+1]='<span style="margin-left:0.2em">×<span style="margin-left:0.1em">10</span></span><s style="display:none">^</s><sup>'ifself.magnitude<0thenret[#ret+1]=MINUS..(-self.magnitude)elseret[#ret+1]=self.magnitudeendret[#ret+1]='</sup>'elseret[#ret+1]=self.signifself.format=='yes'thenret[#ret+1]=Math._precision_format(self.absNumber,self.precision)elselocalnewPrecision=Math._precision(self.absNumber)ret[#ret+1]=tostring(self.absNumber)ifnewPrecision<self.precisionthenifself.integer==self.absNumberthenret[#ret+1]='.'endret[#ret+1]=string.rep('0',math.min(12,self.precision-newPrecision))endendendreturntable.concat(ret)endfunctionNts:makeSortKey()returnSortKey._sortKeyForNumber(self.number)..'♠'endfunctionifNaNThen(n,p)ifisNaN(n)thenreturnpendreturnnendfunctionisNaN(n)returnn~=nendfunctionNts:renderTrackingCategories()ifself.hasDeprecatedParametersthenreturn'[[Category:Nts templates with deprecated parameters]]'elsereturn''endendfunctionNts:__tostring()localroot=mw.html.create()localspan=root:tag('span'):attr('data-sort-value',self:makeSortKey())ifself.debug=='yes'thenspan:tag('span'):css('border','1px solid'):wikitext(self:makeSortKey())elseifself.quiet~='no'thenspan:css('display','none')end-- Displayifself.quiet=='no'thenspan:wikitext(self:makeDisplay())end-- Tracking categoriesroot:wikitext(self:renderTrackingCategories())returntostring(root)end---------------------------------------------------------------------------------- Exports--------------------------------------------------------------------------------localp={}functionp._exportClasses()return{Nts=Nts}endfunctionp._main(args)localsuccess,ret=pcall(function()localnts=Nts.new(args)returntostring(nts)end)ifsuccessthenreturnretelseret=string.format('<strong class="error">Error in [[Template:Nts]]: %s</strong>',ret)ifmw.title.getCurrentTitle().namespace==0then-- Only categorise in the main namespaceret=ret..'[[Category:Nts templates with errors]]'endreturnretendendfunctionp.main(frame)localargs=require('Module:Arguments').getArgs(frame,{wrappers={'Template:Number table sorting'},})returnp._main(args)endreturnp