https://de.wikipedia.org/w/index.php?action=history&feed=atom&title=Modul%3AGapnumModul:Gapnum - Versionsgeschichte2025-06-04T19:59:54ZVersionsgeschichte dieser Seite in WikipediaMediaWiki 1.45.0-wmf.3https://de.wikipedia.org/w/index.php?title=Modul:Gapnum&diff=178362468&oldid=prevNov3rd17: aus engl. Wikipedia, benötigt für Modul Val2018-06-16T08:10:10Z<p>aus engl. Wikipedia, benötigt für Modul Val</p>
<p><b>Neue Seite</b></p><div>local p = {}<br />
<br />
local getArgs<br />
<br />
function p.main(frame)<br />
if not getArgs then<br />
getArgs = require('Module:Arguments').getArgs<br />
end<br />
<br />
local args = getArgs(frame, {wrappers = 'Template:Gapnum'})<br />
local n = args[1]<br />
<br />
if not n then<br />
error('Parameter 1 is required')<br />
elseif not tonumber(n) and not tonumber(n, 36) then -- Validates any number with base ≤ 36<br />
error('Unable to convert "' .. args[1] .. '" to a number')<br />
end<br />
<br />
local gap = args.gap<br />
local precision = tonumber(args.prec)<br />
<br />
return p.gaps(n,{gap=gap,prec=precision})<br />
end<br />
<br />
-- Not named p._main so that it has a better function name when required by Module:Val<br />
function p.gaps(n,tbl)<br />
local nstr = tostring(n)<br />
if not tbl then<br />
tbl = {}<br />
end<br />
local gap = tbl.gap or '.25em'<br />
<br />
local int_part, frac_part = p.groups(n,tbl.prec)<br />
<br />
local ret = mw.html.create('span')<br />
:css('white-space','nowrap')<br />
-- No gap necessary on first group<br />
:wikitext(table.remove(int_part,1))<br />
<br />
-- Build int part<br />
for _, v in ipairs(int_part) do<br />
ret:tag('span')<br />
:css('margin-left',gap)<br />
:wikitext(v)<br />
end<br />
<br />
if frac_part then<br />
-- The first group after the decimal shouldn't have a gap<br />
ret:wikitext('.' .. table.remove(frac_part,1))<br />
-- Build frac part<br />
for _, v in ipairs(frac_part) do<br />
ret:tag('span')<br />
:css('margin-left',gap)<br />
:wikitext(v)<br />
end<br />
end<br />
<br />
return ret<br />
end<br />
<br />
-- Creates tables where each element is a different group of the number<br />
function p.groups(num,precision)<br />
local nstr = tostring(num)<br />
if not precision then<br />
precision = -1<br />
end<br />
<br />
local decimalloc = nstr:find('.', 1, true)<br />
local int_part, frac_part<br />
if decimalloc == nil then<br />
int_part = nstr<br />
else<br />
int_part = nstr:sub(1, decimalloc-1)<br />
frac_part = nstr:sub(decimalloc + 1)<br />
end<br />
-- only define ret_i as an empty table, let ret_d stay nil<br />
local ret_i,ret_d = {}<br />
-- Loop to handle most of the groupings; from right to left, so that if a group has less than 3 members, it will be the first group<br />
while int_part:len() > 3 do<br />
-- Insert in first spot, since we're moving backwards<br />
table.insert(ret_i,1,int_part:sub(-3))<br />
int_part = int_part:sub(1,-4)<br />
end<br />
-- handle any left over numbers<br />
if int_part:len() > 0 then<br />
table.insert(ret_i,1,int_part)<br />
end<br />
<br />
if precision ~= 0 and frac_part then<br />
ret_d = {}<br />
if precision == -1 then<br />
precision = frac_part:len()<br />
end<br />
-- Reduce the length of the string if required precision is less than actual precision<br />
-- OR<br />
-- Increase it (by adding 0s) if the required precision is more than actual<br />
local offset = precision - frac_part:len()<br />
if offset < 0 then<br />
frac_part = frac_part:sub(1,precision)<br />
elseif offset > 0 then<br />
frac_part = frac_part .. string.rep('0', offset)<br />
end<br />
<br />
-- Allow groups of 3 or 2 (3 first)<br />
for v in string.gmatch(frac_part,'%d%d%d?') do<br />
table.insert(ret_d,v)<br />
end<br />
-- Preference for groups of 4 instead of groups of 1 at the end<br />
if #frac_part % 3 == 1 then<br />
if frac_part:len() == 1 then<br />
ret_d = {frac_part}<br />
else<br />
local last_g = ret_d[#ret_d] or ''<br />
last_g = last_g..frac_part:sub(-1)<br />
ret_d[#ret_d] = last_g<br />
end<br />
end<br />
end<br />
<br />
return ret_i,ret_d<br />
end<br />
<br />
return p</div>Nov3rd17