Difference between revisions of "Module:Shorten"

From Pandorabox
Jump to navigation Jump to search
(Created page with "local module = {} --- this is copied straight from technic/technic/helpers.lua local prefixes = {[-8] = "y", [-7] = "z", [-6] = "a", [-5] = "f", [-4] = "p", [-3] = "n", [-2]...")
 
m
Line 7: Line 7:
 
function module.EU(frame)
 
function module.EU(frame)
 
frame = frame or {}
 
frame = frame or {}
local num = frame[1] or 0
+
local num = frame.args[1] or 0
 
-- the small number added is due to floating point inaccuracy
 
-- the small number added is due to floating point inaccuracy
 
local b = math.floor(math.log10(math.abs(num)) +0.000001)
 
local b = math.floor(math.log10(math.abs(num)) +0.000001)
Line 42: Line 42:
  
 
-- shorten like 1.23k
 
-- shorten like 1.23k
local function sign(money) -- helper function for the formatting
+
local function sign(x) -- helper function for the formatting
money = tonumber(money)
+
x = tonumber(x)
if money < 0 then return "-" end
+
if x < 0 then return "-" end
 
return ""
 
return ""
 
end
 
end
Line 54: Line 54:
 
-- format a number
 
-- format a number
 
-- e.g: 1 = $1, 5632 = $5.63k etc.
 
-- e.g: 1 = $1, 5632 = $5.63k etc.
function module.shorten(num)
+
function module.shorten(frame)
 +
frame = frame or {}
 +
local num = frame.args[1] or 0
 
local sign = sign(num)
 
local sign = sign(num)
 
if math.abs(num) < 1000 then  
 
if math.abs(num) < 1000 then  

Revision as of 19:11, 8 April 2024

Documentation for this module may be created at Module:Shorten/doc

local module = {}

--- this is copied straight from technic/technic/helpers.lua
local prefixes = {[-8] = "y", [-7] = "z", [-6] = "a", [-5] = "f", [-4] = "p",
	[-3] = "n", [-2] = "µ", [-1] = "m", [0] = "",  [1] = "k", [2] = "M",
	[3] = "G", [4] = "T", [5] = "P", [6] = "E", [7] = "Z", [8] = "Y"}
function module.EU(frame)
	frame = frame or {}
	local num = frame.args[1] or 0
	-- the small number added is due to floating point inaccuracy
	local b = math.floor(math.log10(math.abs(num)) +0.000001)
	local pref_i
	if b ~= 0 then
		-- b is decremented by 1 to avoid a single digit with many decimals,
		-- e.g. instead of 1.021 MEU, 1021 kEU is shown
		pref_i = math.floor((b - 1) / 3)
	else
		-- as special case, avoid showing e.g. 1100 mEU instead of 1.1 EU
		pref_i = 0
	end
	if not prefixes[pref_i] then
		-- This happens for 0, nan, inf, very big values, etc.
		if num == 0 then
			-- handle 0 explicilty to avoid showing "-0"
			if not constant_digit_count then
				return "0 "
			end
			-- gives 0.000
			return string.format("%.3f ", 0)
		end
		return string.format("%.4g ", num)
	end

	num = num * 10 ^ (-3 * pref_i)
	if 4 then
		local comma_digits_cnt = 3 - (b - 3 * pref_i)
		return string.format("%." .. comma_digits_cnt .. "f %s",
			num, prefixes[pref_i])
	end
	return string.format("%.4g %s", num, prefixes[pref_i]).."EU"
end

-- shorten like 1.23k
local function sign(x) -- helper function for the formatting
	x = tonumber(x)
	if x < 0 then return "-" end
	return ""
end
local function trim(x,y)
	y = 10^y
	return math.floor(x*y)/y
end

-- format a number
-- e.g: 1 = $1, 5632 = $5.63k etc.
function module.shorten(frame)
	frame = frame or {}
	local num = frame.args[1] or 0
	local sign = sign(num)
	if math.abs(num) < 1000 then 
		if money == 0 then return "0" end
		return num
	end
	if math.abs(num) == math.huge then return "Infinity" end
	if num ~= num then return num end
	local shortlist = {[0] = "","k", "m", "b", "t"} -- until decillions, which is honestly enough
	for _, a in ipairs({"","ce"}) do
		for _, b in ipairs({"","de","vi","tg","qag","qig","sxg","spg","ocg","ng"}) do
			for _, c in ipairs({"","u","d","t","qa","qi","sx","sp","oc","no"}) do
				table.insert(shortlist,c..b..a)
			end
		end
	end
	table.remove(shortlist,5); table.remove(shortlist,5); table.remove(shortlist,5)
	local suffixid = math.floor((math.log(num)/math.log(1000))) -- funky way of doing log1000
	
	local x = num/(1000^suffixid)
	local y = math.floor(math.log10(num)%3)
	if y == 0 then -- case: 1.23k
		x = trim(x,2)
	elseif y == 1 then -- case: 12.3k
		x = trim(x,1)
	else -- case: 123k
		x = math.floor(x)
	end
	
	local out = sign..x..shortlist[suffixid]
end

return module