https://de.wikipedia.org/w/index.php?action=history&feed=atom&title=Modul%3AGapnum Modul:Gapnum - Versionsgeschichte 2025-06-04T19:59:54Z Versionsgeschichte dieser Seite in Wikipedia MediaWiki 1.45.0-wmf.3 https://de.wikipedia.org/w/index.php?title=Modul:Gapnum&diff=178362468&oldid=prev Nov3rd17: aus engl. Wikipedia, benötigt für Modul Val 2018-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(&#039;Module:Arguments&#039;).getArgs<br /> end<br /> <br /> local args = getArgs(frame, {wrappers = &#039;Template:Gapnum&#039;})<br /> local n = args[1]<br /> <br /> if not n then<br /> error(&#039;Parameter 1 is required&#039;)<br /> elseif not tonumber(n) and not tonumber(n, 36) then -- Validates any number with base ≤ 36<br /> error(&#039;Unable to convert &quot;&#039; .. args[1] .. &#039;&quot; to a number&#039;)<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 &#039;.25em&#039;<br /> <br /> local int_part, frac_part = p.groups(n,tbl.prec)<br /> <br /> local ret = mw.html.create(&#039;span&#039;)<br /> :css(&#039;white-space&#039;,&#039;nowrap&#039;)<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(&#039;span&#039;)<br /> :css(&#039;margin-left&#039;,gap)<br /> :wikitext(v)<br /> end<br /> <br /> if frac_part then<br /> -- The first group after the decimal shouldn&#039;t have a gap<br /> ret:wikitext(&#039;.&#039; .. table.remove(frac_part,1))<br /> -- Build frac part<br /> for _, v in ipairs(frac_part) do<br /> ret:tag(&#039;span&#039;)<br /> :css(&#039;margin-left&#039;,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(&#039;.&#039;, 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() &gt; 3 do<br /> -- Insert in first spot, since we&#039;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() &gt; 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 &lt; 0 then<br /> frac_part = frac_part:sub(1,precision)<br /> elseif offset &gt; 0 then<br /> frac_part = frac_part .. string.rep(&#039;0&#039;, offset)<br /> end<br /> <br /> -- Allow groups of 3 or 2 (3 first)<br /> for v in string.gmatch(frac_part,&#039;%d%d%d?&#039;) 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 &#039;&#039;<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