有兽档案馆
搜索
切换搜索
切换菜单
切换个人菜单
查看“Module:Number table sorting”的源代码
来自有兽档案馆
阅读
查看源代码
查看历史
模块
讨论
更多操作
←
Module:Number table sorting
因为以下原因,您没有权限编辑该页面:
您所请求的操作,仅限具有
注册用户
、
自动确认用户
、
管理员
、
界面管理员
、
优质编辑者
、
确认用户
、
巡查员
、
档案理事员
等
权限
的
用户
执行。
若您尚未登录有兽档案馆账号,请您
登录
有兽档案馆账号后操作。
您尚未完成电子邮件确认,因此操作受限,请尽快
完成电子邮件确认
。
若您无法完成前述手续,请通过适当渠道请求管理员或档案理事员协助。
您可以查看和复制此页面的源代码。
若您无权编辑本页面,您可以
提出编辑请求
,提请有权限者代为编辑。
local lang = mw.language.getContentLanguage() local Math = require('Module:Math') local SortKey = require('Module:Sortkey') -- constants local INF = math.huge local NEGINF = -math.huge local MINUS = '−' -- Unicode U+2212 MINUS SIGN (UTF-8: e2 88 92) -------------------------------------------------------------------------------- -- Nts class -------------------------------------------------------------------------------- local Nts = {} Nts.__index = Nts Nts.formats = { no = true, yes = true, } function Nts.new(args) local self = setmetatable({}, Nts) self:parseNumber(args[1]) -- Set the format string self.format = args.format or 'yes' if not Nts.formats[self.format] then error(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.00123 self.prefix = args.prefix or '' -- debug info self.debug = args.debug or 'no' self.quiet = args.quiet or 'no' return self end -- Parse the entered number function Nts:parseNumber(s) -- sanitize s = s or ''; s = string.gsub(s,'−','-') s = string.gsub(s, MINUS, '-') self.rawNumberString = s -- fractions. was somewhat but completely broken at some point self.isFraction = (string.find(s, '/') ~= nil) if self.isFraction then error(string.format( "不支持分數(fractions)", tostring(s) ), 0) end -- format detection self.isScientificNotation = (string.find(s, 'e') ~= nil) -- parse with language options self.number = lang:parseFormattedNumber(s) -- parse with fallback if not self.number then self.number = tonumber(s) end -- allow for empty string as a value if not self.number then -- error(string.format( -- "'%s' is not a valid number", -- tostring(s) -- ), 0) self.number = NEGINF end if self.number < 0 then self.sign = MINUS else self.sign = '' end self.absNumber = math.abs(self.number) if self.absNumber ~= INF then self.magnitude = math.floor(math.log10(self.absNumber)) self.significand = self.number / 10^self.magnitude self.precision = Math._precision(self.rawNumberString) self.integer = math.floor(self.absNumber) self.fractional = math.abs(self.number - self.integer) end end function Nts:makeDisplay() local ret ={} if self.quiet == 'yes' then return '' end ret[#ret + 1] = self.prefix local sciNotation = string.find(tostring(self.number),'e') if self.absNnumber == INF or isNaN(self.number) or self.magnitude ==nil or math.abs(self.magnitude) == INF then ret[#ret + 1] = string.gsub(self.rawNumberString, '-', MINUS) elseif sciNotation ~= nil or math.abs(self.magnitude) >= 9 then ret[#ret + 1] = self.sign if self.format == 'yes' then ret[#ret + 1] = lang:formatNum(math.abs(self.number * 10^-self.magnitude)) else ret[#ret + 1] = math.abs(self.number * 10^-self.magnitude) end ret[#ret + 1] = '<span style="margin-left:0.2em">×<span style="margin-left:0.1em">10</span></span><s style="display:none">^</s><sup>' if self.magnitude<0 then ret[#ret + 1] = MINUS .. (-self.magnitude) else ret[#ret + 1] = self.magnitude end ret[#ret + 1] = '</sup>' else ret[#ret + 1] = self.sign if self.format == 'yes' then ret[#ret + 1] = Math._precision_format(self.absNumber, self.precision) else local newPrecision = Math._precision(self.absNumber) ret[#ret + 1] = tostring(self.absNumber) if newPrecision < self.precision then if self.integer == self.absNumber then ret[#ret + 1] = '.' end ret[#ret + 1] = string.rep('0', math.min(12, self.precision - newPrecision) ) end end end return table.concat(ret) end function Nts:makeSortKey() return SortKey._sortKeyForNumber(self.number) .. '♠' end function ifNaNThen(n,p) if isNaN(n) then return p end return n end function isNaN(n) return n ~= n end function Nts:renderTrackingCategories() if self.hasDeprecatedParameters then return '[[Category:使用過時參數的Nts模板]]' else return '' end end function Nts:__tostring() local root = mw.html.create() local span = root:tag('span') :attr('data-sort-value', self:makeSortKey()) if self.debug == 'yes' then span:tag('span') :css('border', '1px solid') :wikitext(self:makeSortKey()) elseif self.quiet ~= 'no' then span:css('display', 'none') end -- Display if self.quiet == 'no' then span:wikitext(self:makeDisplay()) end -- Tracking categories root:wikitext(self:renderTrackingCategories()) return tostring(root) end -------------------------------------------------------------------------------- -- Exports -------------------------------------------------------------------------------- local p = {} function p._exportClasses() return { Nts = Nts } end function p._main(args) local success, ret = pcall(function () local nts = Nts.new(args) return tostring(nts) end) if success then return ret else ret = string.format( '<strong class="error">[[Template:Nts]]有錯誤:%s</strong>', ret ) if mw.title.getCurrentTitle().namespace == 0 then -- Only categorise in the main namespace ret = ret .. '[[Category:有錯誤的Nts模板]]' end return ret end end function p.main(frame) local args = require('Module:Arguments').getArgs(frame, { wrappers = { 'Template:Number table sorting' }, }) return p._main(args) end return p
该页面使用的模板:
Module:Arguments
(
查看源代码
)
Module:Documentation
(
查看源代码
)
Module:Fullurl
(
查看源代码
)
Module:Math
(
查看源代码
)
Module:No globals
(
查看源代码
)
Module:Number table sorting
(
查看源代码
)
Module:Number table sorting/doc
(
查看源代码
)
Module:Page name
(
查看源代码
)
Module:Sortkey
(
查看源代码
)
Module:Yesno
(
查看源代码
)
返回
Module:Number table sorting
。