Module:Val: Difference between revisions

Content deleted Content added
update from sandbox; val/sandbox will call convert/sandbox
change 0.3em spacing to 0.225em (same total space as old code) per Template talk:Val#Proposal to reduce spacing around ±
 
(2 intermediate revisions by the same user not shown)
Line 1:
-- For Template:Val, output a number and optional unit.
-- Format options include scientific and uncertainty notations.
 
local numdot = '.' -- decimal mark (use ',' for Italian)
local numsep = ',' -- group separator (use ' ' for Italian)
local mtext = {
-- Message and other text that should be localized.
['mt-bad-exponent'] = 'exponent parameter (<b>e</b>)',
['mt-parameter'] = 'parameter ',
['mt-not-number'] = 'is not a valid number',
['mt-cannot-range'] = 'cannot use a range if the first parameter includes "e"',
['mt-need-range'] = 'needs a range in parameter 2',
['mt-should-range'] = 'should be a range',
['mt-cannot-with-e'] = 'cannot be used if the first parameter includes "e"',
['mt-not-range'] = 'does not accept a range',
['mt-cannot-e'] = 'cannot use e notation',
['mt-too-many-parameter'] = 'too many parameters',
['mt-need-number'] = 'need a number after the last parameter because it is a range.',
['mt-ignore-parameter4'] = 'Val parameter 4 ignored',
['mt-val-not-supported'] = 'Val parameter "%s=%s" is not supported',
['mt-invalid-scale'] = 'Unit "%s" has invalid scale "%s"',
['mt-both-u-ul'] = 'unit (<b>u</b>) and unit with link (<b>ul</b>) are both specified, only one is allowed.',
['mt-both-up-upl'] = 'unit per (<b>up</b>) and unit per with link (<b>upl</b>) are both specified, only one is allowed.',
}
 
local data_module = 'Module:Val/units'
Line 59 ⟶ 81:
-- Input like 1e3 is regarded as invalid for all except argument 1
-- which accepts e notation as an alternative to the 'e' argument.
-- Input commasgroup separators are removed so 1,234 is the same as 1234.
local which = index
local function fail(msg)
local description
if which == 'e' then
description = mtext['mt-bad-exponent parameter (<b>e</b>)']
else
description = mtext['mt-parameter '] .. which
end
return description .. ' ' .. (msg or mtext['is mt-not a valid -number']) .. '.'
end
local result = {}
Line 76 ⟶ 98:
if index == 2 then
if numbers[1] and numbers[1].exp then
return fail(mtext['mt-cannot use a -range if the first parameter includes "e"'])
end
numbers.has_ranges = true
else
if not numbers.has_ranges then
return fail(mtext['needs a mt-need-range in parameter 2'])
end
end
Line 91 ⟶ 113:
return nil
end
return fail(mtext['does mt-not accept a -range'])
end
if numbers.has_ranges and type(index) == 'number' and (index % 2 == 0) then
return fail(mtext['mt-should be a -range'])
end
if index == 'e' then
Line 100 ⟶ 122:
if e then
if arg then
return fail(mtext['mt-cannot be used if the first parameter includes "-with-e"'])
end
arg = e
Line 107 ⟶ 129:
end
if arg and arg ~= '' then
arg = arg:gsub(','numsep, '')
if numdot ~= '.' then
arg = arg:gsub(numdot, '.')
end
if arg:sub(1, 1) == '(' and arg:sub(-1) == ')' then
result.parens = true
Line 118 ⟶ 143:
result.exp = b
else
return fail(mtext['mt-cannot use -e notation'])
end
end
Line 172 ⟶ 197:
end
if index > 19 then
return mtext['mt-too -many parameters-parameter']
end
end
if numbers.has_ranges and (#numbers % 2 == 0) then
return mtext['mt-need a -number after the last parameter because it is a range.']
end
end
Line 206 ⟶ 231:
end
end
error('Unit "' string.. ucode .. format(mtext['" has mt-invalid -scale "'], ..ucode, text .. '"'))
end
 
Line 514 ⟶ 539:
local delimit_groups = require('Module:Gapnum').groups
local function delimit(sign, numstr, fmt)
-- Return sign and numstr (unsigned digits or '.'numdot only) after formatting.
-- Four-digit integers are not formatted with gaps.
fmt = (fmt or ''):lower()
Line 525 ⟶ 550:
local result
if fmt == 'commas' then
result = sign .. table.concat(ipart, ','numsep)
if dpart then
result = result .. '.'numdot .. table.concat(dpart)
end
else
Line 537 ⟶ 562:
end
if dpart then
table.insert(groups, '.'numdot .. (table.remove(dpart, 1) or ''))
for _, v in ipairs(dpart) do
table.insert(groups, '<span style="margin-left:.25em;">' .. v .. '</span>')
Line 604 ⟶ 629:
else
text = (angle or '') ..
'<span style="margin-left:0.3em225em;margin-right:0.15em225em;">±</span>' ..
delimit('', uncU, fmt)
need_parens = true
Line 748 ⟶ 773:
for k, v in pairs(args) do
if type(k) == 'string' and not whitelist[k] then
local warning = string.format(mtext['Val parameter "mt-val-not-supported' ..], k .. '=' .., v .. '" is not supported')
return valerror(warning, nocat, true)
end
end
if not has_ranges and args[4] then
return valerror(mtext['Val parameter 4 ignoredmt-ignore-parameter4'], nocat, true)
end
end
Line 767 ⟶ 792:
end
if args.u and args.ul then
return valerror(mtext['unit (<b>mt-both-u</b>) and unit with link (<b>-ul</b>) are both specified, only one is allowed.'], nocat)
end
if args.up and args.upl then
return valerror(mtext['unit per (<b>mt-both-up</b>) and unit per with link (<b>-upl</b>) are both specified, only one is allowed.'], nocat)
end
local values