Jump to content

Module:Sandbox/RexxS: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
demo function numbers
roundto function demo
Line 22: Line 22:
end
end


function p.numbers(frame)
p.roundto = function(frame)
local x = tonumber(frame.args[1]) or 30
local x = frame.args.x
local y = x / 3
local sf = frame.args.sf
if x == 0 then return 0 end
local z = math.floor(y)
local s = 1
return "x = " .. x .. "<br>" .. "y = " .. y .. "<br>" .. "z = ".. z
if x < 0 then
x = -x
s = -1
end
if sf < 1 then sf = 1 end
local p = 10 ^ (math.floor(math.log10(x)) - sf + 1)
x = math.floor(x / p + 0.5) * p * s
-- if it's integral, cast to an integer:
if x == math.floor(x) then x = math.floor(x) end
return x
end
end



Revision as of 23:28, 16 December 2020

-- This is here as a placeholder.
-- It is to demonstrate to Google-Code-in students what the module sandbox is.

local p = {}

function p.cats(frame)
	local title = frame.args.title or ""
	local ttlobj = mw.title.new( title, 14 )
	if ttlobj then
		local cont = ttlobj:getContent()
		if cont then return cont:sub( 1, 100000 ) end
	end
end

function p.namespaces(frame)
	local ns = mw.site.namespaces
	local msg = ""
	for k, v in pairs(ns) do
		msg = msg .. k .. " = " .. v.name .. "<br>"
	end
	return msg
end

p.roundto = function(frame)
	local x = frame.args.x
	local sf = frame.args.sf
	if x == 0 then return 0 end
	local s = 1
	if x < 0 then
		x = -x
		s = -1
	end
	if sf < 1 then sf = 1 end
	local p = 10 ^ (math.floor(math.log10(x)) - sf + 1)
	x = math.floor(x / p + 0.5) * p * s
	-- if it's integral, cast to an integer:
	if x == math.floor(x) then x = math.floor(x) end
	return x
end

return p