Module:Val

local p = {} local getArgs local delimit_groups = require('Module:Gapnum').groups local makeunit = require('Module:Val/units') local mSu = require('Module:Su')._main

function p.main(frame) if not getArgs then getArgs = require('Module:Arguments').getArgs end local args = getArgs(frame, {wrappers = 'Template:Val'}) local number = {n=args[1], nend=args['end']} local uncertainty = {upper=args[2], lower=args[3], errend=args.errend, upperend=args['+errend'], lowerend=args['-errend']} local u_tbl = {u=args.ul or args.u, ul=args.ul ~= nil, p=args.upl or args.up, pl=args.upl ~= nil} local misc_tbl = {e=args.e, pre=args.p, suf=args.s, fmt=args.fmt or '', nocat=args.nocategory} return p._main(number,uncertainty,u_tbl,misc_tbl) end

function p._main(number,uncertainty,u_tbl,misc_tbl) local fmt = misc_tbl.fmt local n = delimit(number.n,fmt) local unc local uncU, uncL = uncertainty.upper, uncertainty.lower if number.nend then n:wikitext(number.nend) end

if uncU then if uncL then uncU = delimit(uncU,fmt)..(uncertainty.upperend or '') uncL = delimit(uncL,fmt)..(uncertainty.lowerend or '') unc = ' '..mSu(uncU,uncL)..' ' else local uncU_n = mw.ustring.match(uncU,('%((.+)%)')) or uncU if uncU == uncU_n then unc = ' ± '..delimit(uncU_n,fmt) if uncertainty.errend then unc = unc..errend end unc = unc..' ' else unc = '('..delimit(uncU_n,fmt)..')' end end end if u_tbl.u then units = makeunit(u_tbl.u,						{link=u_tbl.ul,							per=u_tbl.p,							per_link=u_tbl.pl}) end local ret = { misc_tbl.pre or '', misc_tbl.e and '(' or ,				tostring(n),				misc_tbl.nend or ,				unc or ,				misc_tbl.e and ')' or , misc_tbl.e and ' × 10'..delimit(misc_tbl.e).. or , units or '', misc_tbl.suf or '' }	ret = table.concat(ret) return ret end

-- TODO: Add other format options function delimit(n,fmt) local prefix,num if not fmt then fmt = '' end if n:find('[%-%+]') then prefix,num = mw.ustring.match(n,'([-+])([%d.]+)') else num = n	end local ipart, dpart = delimit_groups(num) if fmt:lower == 'commas' then num = table.concat(ipart,',') if dpart then dpart = table.concat(dpart) num = num..'.'..dpart end else num = {} num[1] = table.remove(ipart,1) for _, v in ipairs(ipart) do			table.insert(num,' '..v..' ') end if dpart then table.insert(num,'.'..table.remove(dpart,1)) for _, v in ipairs(dpart) do				table.insert(num,' '..v..' ') end end num = table.concat(num) end if prefix then if prefix == '-' then prefix = '&minus;' end num = prefix..num end return num end

return p