local e_10 = options.e or {}
local novalue = (number.value == nil and e_10.clean == nil)
local fmt = options.fmt
local want_sort = true
local sortable = options.sortable
if sortable == 'off' or (sortable == nil and novalue) then
want_sort = false
elseif sortable == 'debug' then
-- Same as sortable = 'on' but the sort key is displayed.
else
sortable = 'on'
end
local sort_value = 1
if want_sort then
sort_value = number.value or 1
if e_10.value and sort_value ~= 0 then
-- The 'if' avoids giving an invalid sort_value due to overflow.
sort_value = sort_value * 10^e_10.value
end
end
local unit_table = makeunit(sort_value, {
u = unit_spec.u,
want_link = unit_spec.want_link,
per = unit_spec.per,
want_per_link = unit_spec.want_per_link,
nospace = novalue,
want_longscale = unit_spec.want_longscale,
sortable = sortable,
})
local sortkey
if unit_table then
if want_sort then
sortkey = unit_table.sortkey
end
else
unit_table = { text = '' }
if want_sort then
sortkey = convert_lookup('dummy', sort_value, nil, nil, nil, { sortable = sortable }).sortspan
end
end
local final_unit = unit_table.isangle and '' or unit_table.text
local e_text, n_text, need_parens
local uncertainty = values.uncertainty
if uncertainty then
if number.clean then
n_text = delimit(number.sign, number.clean, fmt) .. (number.nend or '')
local text
text, need_parens = uncertainty_text(uncertainty, unit_table, options)
if text then
n_text = n_text .. text
end
else
n_text = ''
end
else
if values.numbers.isrepeat then
final_unit = ''
end
n_text = range_text(values.numbers, unit_table, options)
need_parens = true
end
if e_10.clean then
if need_parens then
n_text = '(' .. n_text .. ')'
end
e_text = '10<sup>' .. delimit(e_10.sign, e_10.clean, fmt) .. '</sup>'
if number.clean then
e_text = '<span>×</span>' .. e_text
end
else
e_text = ''
end
local result =
(sortkey or '') ..
(options.prefix or '') ..
n_text ..
e_text ..
final_unit ..
(options.suffix or '')
if result ~= '' then
result = '<span>' .. result .. '</span>'
end
return result .. (options.warning or '')
end  
local function check_parameters(args, has_ranges, nocat)
-- Return warning text for the first problem parameter found, or nothing if ok.
local whitelist = {
a = true,
action = true,
debug = true,
e = true,
['end'] = true,
errend = true,
['+errend'] = true,
['-errend'] = true,
fmt = true,
['long scale'] = true,
long_scale = true,
longscale = true,
nocategory = true,
p = true,
s = true,
sortable = true,
u = true,
ul = true,
up = true,
upl = true,
}
for k, v in pairs(args) do
if type(k) == 'string' and not whitelist[k] then
local warning = 'Val parameter "' .. k .. '=' .. v .. '" is not supported'
return valerror(warning, nocat, true)
end
end
if not has_ranges and args[4] then
return valerror('Val parameter 4 ignored', nocat, true)
end
end  
local function main
local getArgs = require('Module:Arguments').getArgs
local args = getArgs(, {wrappers = { 'Template:Val' }})
local nocat = args.nocategory
local numbers = {} -- table of number tables, perhaps with range text
local msg = get_args(numbers, args)
if msg then
return valerror(msg, nocat)
end
if args.u and args.ul then
return valerror('unit (<b>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('unit per (<b>up</b>) and unit per with link (<b>upl</b>) are both specified, only one is allowed.', nocat)
end
local values
if numbers.has_ranges then
-- Multiple values with range separators but no uncertainty.
numbers.nend = args['end']
values = {
numbers = numbers,
}
else
-- A single value with optional uncertainty.
local function setfield(i, dst, src)
local v = args[src]
if v then
if numbers[i] then
numbers[i][dst] = v
else
numbers[i] = { [dst] = v }
end
end
end
setfield(1, 'nend', 'end')
setfield(2, 'errend', '+errend')
setfield(3, 'errend', '-errend')
values = {
number = numbers[1],
uncertainty = {
upper = numbers[2],
lower = numbers[3],
errend = args.errend,
}
}
end
local unit_spec = {
u = args.ul or args.u,
want_link = args.ul ~= nil,
per = args.upl or args.up,
want_per_link = args.upl ~= nil,
want_longscale = (args.longscale or args.long_scale or args['long scale']) == 'on',
}
local options = {
action = args.action,
align = args.a,
e = numbers.e,
fmt = args.fmt,
nocat = nocat,
prefix = args.p,
sandbox = string.find(:getTitle, 'sandbox', 1, true) ~= nil,
sortable = args.sortable or (args.debug == 'yes' and 'debug' or nil),
suffix = args.s,
warning = check_parameters(args, numbers.has_ranges, nocat),
}
return _main(values, unit_spec, options)
end  
return { main = main, _main = _main }