Changes

Jump to: navigation, search

Module:Convert

330 bytes added, 9 years ago
update from sandbox per [[Template talk:Convert#Module version 11]]
end
local function digit_grouperdigit_groups(parms, text, method, gaps) -- Return a numbered table to hold of groups of digits which can be joined with -- suitable separators (such as commas). left-to- Each group is separately translated to the right, in local language because -- gap separators include digits which should not be translated).
-- Parameter method is a number or nil:
-- 3 for 3-digit grouping(default), or -- 2 for 3-then-2 grouping(only for digits before decimal mark). local len_right local len_left = text:find('.', 1, true) if len_left then len_right = #text -len_left len_left = len_left - Parameter gaps is true to use <span1 else len_left = #text end local twos = method == 2 and len_left > gaps 5 local groups = collection(numsep ignored) local run = len_left local n if run < 4 or (run == 4 and parms.opt_comma5) then if parms.opt_gaps then n = run else n = #text end return {elseif twos then n = run % 2 == 0,and 1 or 2 else add n = function run % 3 == 0 and 3 or run % 3 end while run > 0 do groups:add(self, digitsn) self.n run = self.run - n + 1 self[self. n] = from_en(digitstwos and run > 3)and 2 or 3 end, if len_right then join if groups.n = function = 0 then groups:add(self, rhs0) end if parms.opt_gaps and len_right > 3 then local want4 = not parms.opt_gaps3 -- Concatenate in reverse order.true gives no gap before trailing single digit if gaps then local result isfirst = ''true for i run = 1, self.n - 1 len_right while run > 0 do result n = '<span style(want4 and run ="margin-left: 0.25em"= 4) and 4 or (run >' .. self3 and 3 or run) if isfirst then isfirst = false groups[igroups.n] = groups[groups.. '</span>' .. resultn] + 1 + n else groups:add(n)
end
return '<span stylerun ="whiterun -space: nowrap">' .. self[self.n] .. result .. from_en(rhs) .. '</span>' else local result = self[1] for i = 2, self.n do result = self[i] .. numsep .. result end return result .. from_en(rhs)
end
end,else step groups[groups.n] = 3,groups[groups.n] + 1 + len_right next_position = function (self, previous)end -- Return position of digit just before next group. end -- Digits are grouped from right-to-left (least significant first). local result pos = previous - self.step1 if method == 2 then for i, length in ipairs(groups) do self.step groups[i] = 2 end return from_en(result < 0text:sub(pos, pos + length - 1)) and 0 or result pos = pos + length end, }return groups
end
function with_separator(parms, text) -- for forward declaration above
-- Input text is a number in en digits and with optional '.' decimal mark. -- Return an equivalent of text, formatted for display:
-- with a custom decimal mark instead of '.', if wanted
-- with thousand separators inserted, if wanted
-- digits in local language
-- The given text is like '123' or '12345123.6789' or '112345.23e456789' -- (at one time e-notation could occur when processing an input value, -- but is now handled elsewhere for scientific notation).
-- The text has no sign (caller inserts that later, if necessary).
-- Separator is When using gaps, they are inserted only in before and after the integer part of the significanddecimal mark. -- (not after Separators are inserted only before the decimal mark, and not after 'e' or 'E'). if #text < 4 or parms.opt_nocomma or numsep == '' then
return from_en(text)
end
local last groups = digit_groups(parms, text:match('(, group_method)[.eE]') -- () returns position if last == nil parms.opt_gaps then last = #text else last = last - 1 -- index of last character before dot/e/E end if last groups.n < 4 or (last == 4 and parms.opt_comma5) 1 then return from_en(text)groups[1] or '' end local groups nowrap = digit_grouper(group_method, parms.opt_gaps)'<span style="white-space: nowrap">' local i gap = last while i > '<span style="margin-left: 0 do.25em">' local position close = groups:next_position(i)'</span>' return nowrap .. groups:add[1] .. gap .. table.concat(text:sub(position+1groups, i)close .. gap, 2, groups.n) i = position.. close .. close
end
return table.concat(groups:join(text:sub(last+1), numsep)
end
local fmtpower = '%s<span style="margin:0 .15em 0 .25em">×</span>%s<sup>%s</sup>'
local function with_exponent(parms, show, exponent)
-- Return wikitext to display the implied value in scientific notation.
-- Input uses en digits; output uses digits in local language.
return format(fmtpower, from_enwith_separator(parms, show), from_en('10'), use_minus(from_en(tostring(exponent))))
end
exponent = exponent,
sign = sign,
show = sign .. with_exponent(parms, significand, exponent-1),
is_scientific = true,
}
-- or return false, t where t is an error message table.
-- Input can use en digits or digits in local language and can
-- have one reference references at the end. Accepting a reference references is intended
-- for use in infoboxes with a field for a value passed to convert.
-- Parameter another = true if the expected value is not the first.
-- clean = cleaned text with any separators and sign removed
-- (en digits and '.' decimal mark)
-- show = text formatted for output, possibly with ref strip markermarkers
-- (digits in local language and custom decimal mark)
-- The resulting show:
-- '+' (if the input text used '+'), or is '' (if no sign in input).
text = strip(text or '')
local t, reference local pos = text:matchfind('^\127', 1, true) if pos then local before = text:sub(.*1, pos - 1) local remainder = text:sub(pos) local refs = {} while #remainder > 0 do local ref, spaces ref, spaces, remainder = remainder:match('^(\127UNIQ%x+[^\127]*%-ref%-%x+%-QINU\127)$(%s*)(.*)') if reference ref then -- found a single strip marker at end containing "- table.insert(refs, ref-") else refs = {} break end end if #refs > 0 then text = strip(tbefore) reference = table.concat(refs) end
end
local clean = to_en(text, parms)
local significand, exponent = clean:match('^([%d.]+)[Ee]([+%-]?%d+)')
if significand then
show = with_exponent(parms, rounded(significand, significand), exponent)
scientific = true
else
end
info.decorated = true
end if engscale then local inout = unit_table.inout local abbr = parms.abbr if abbr == 'on' or abbr == inout then info.show = info.show ..
'<span style="margin-left:0.2em">×<span style="margin-left:0.1em">' ..
from_en('10') ..
'</span></span><s style="display:none">^</s><sup>' ..
from_en(tostring(engscale.exponent)) .. '</sup>'
else
local number_id
local lk = parms.lk
if lk == 'on' or lk == inout then
number_id = make_link(engscale.link, engscale[1])
else
local number_id local lk = parms.lk if lk == 'on' or lk == inout then number_id = make_link(engscale.link, engscale[1]) else number_id = engscale[1] end -- WP:NUMERAL recommends "&nbsp;" in values like "12 million". info.show = info.show .. (parms.opt_adjectival and '-' or '&nbsp;') .. number_id
end
-- WP:NUMERAL recommends "&nbsp;" in values like "12 million".
info.show = info.show .. (parms.opt_adjectival and '-' or '&nbsp;') .. number_id
end
end if prefix then info.show = prefix .. info.show end
end
end
Anonymous user