Modul:Gapnum
Qiyofa
Bu modul uchun Modul:Gapnum/doc nomli hujjat sahifasini yaratishingiz mumkin
local p = {}
local getArgs
function p.main(frame)
if not getArgs then
getArgs = require('Module:Arguments').getArgs
end
local args = getArgs(frame, {wrappers = 'Andoza:Gapnum'})
local n = args[1]
if not n then
error('Parametr 1 talab etiladi')
elseif not tonumber(n) and not tonumber(n, 36) then
error('Unable to convert "' .. args[1] .. '" to a number')
end
local gap = args.gap
local precision = tonumber(args.prec)
return p.gaps(n,{gap=gap,prec=precision})
end
function p.gaps(n,tbl)
local nstr = tostring(n)
if not tbl then
tbl = {}
end
local gap = tbl.gap or '.25em'
local int_part, frac_part = p.groups(n,tbl.prec)
local ret = mw.html.create('span')
:css('white-space','nowrap')
:wikitext(table.remove(int_part,1))
for _, v in ipairs(int_part) do
ret:tag('span')
:css('margin-left',gap)
:wikitext(v)
end
if frac_part then
ret:wikitext('.' .. table.remove(frac_part,1))
for _, v in ipairs(frac_part) do
ret:tag('span')
:css('margin-left',gap)
:wikitext(v)
end
end
return ret
end
function p.groups(num,precision)
local nstr = tostring(num)
if not precision then
precision = -1
end
local decimalloc = nstr:find('.', 1, true)
local int_part, frac_part
if decimalloc == nil then
int_part = nstr
else
int_part = nstr:sub(1, decimalloc-1)
frac_part = nstr:sub(decimalloc + 1)
end
local ret_i,ret_d = {}
while int_part:len() > 3 do
table.insert(ret_i,1,int_part:sub(-3))
int_part = int_part:sub(1,-4)
end
if int_part:len() > 0 then
table.insert(ret_i,1,int_part)
end
if precision ~= 0 and frac_part then
ret_d = {}
if precision == -1 then
precision = frac_part:len()
end
local offset = precision - frac_part:len()
if offset < 0 then
frac_part = frac_part:sub(1,precision)
elseif offset > 0 then
frac_part = frac_part .. string.rep('0', offset)
end
for v in string.gmatch(frac_part,'%d%d%d?') do
table.insert(ret_d,v)
end
if #frac_part % 3 == 1 then
if frac_part:len() == 1 then
ret_d = {frac_part}
else
local last_g = ret_d[#ret_d] or ''
last_g = last_g..frac_part:sub(-1)
ret_d[#ret_d] = last_g
end
end
end
return ret_i,ret_d
end
return p