Jump to content

Module:NumberAbbr

From Keynotathlon Wiki
Revision as of 17:31, 22 August 2025 by MisterChuChu (talk | contribs) (MisterChuChu moved page Module:NumberAbbr to Module:NumberAbbreviate)

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

p={}
function p.abbr(frame)
	local f=frame
	local n=f.args[1]~=nil and tostring(f.args[1]) or 0 --number to abbreviate
	local dec=f.args[2]~=nil and math.min(math.max(f.args[2],0),9) or 3 --decimal places
	
	local nn="" --create additional variables
	local ep=0
	local isinf=false
	
	local m=string.sub(n,1,1)=="-" and 1 or 0 --check if negative
	n=string.sub(n,m+1,#n) --if so, remove minus at start
	
	if string.sub(n,1,1)=="i" then --check if infinite
		nn=math.huge
		isinf=true
	else
		for nc in string.gmatch(n,"[1234567890e%.%-%+]+") do --extract valid numerical characters
			nn=nn..nc
		end
		
		
		ep=string.find(nn,"e")~=nil and tonumber(string.sub(nn,string.find(nn,"e")+1,#nn)) or #nn>100 and #nn or math.floor(math.log10(nn)) --get exponent
		
		if string.sub(ep,1,1)=="i" then --check if exponent is infinite
			nn=math.huge
			isinf=true
		else
			nn=string.find(nn,"e")~=nil and string.sub(nn,1,string.find(nn,"e")-1) or nn --remove the exponent from number variable
			
			if tonumber(nn)>999 then --pad variable to 14 characters
				if #nn>14 then
					nn=string.sub(nn,1,14)
				else
					for i=#nn,13 do
						nn=nn.."0"
					end
				end
			end
			
			nn=tonumber(nn)>999 and tonumber(nn)/10^(13-ep%3) or nn
			nn=math.floor(nn*10^dec)/10^dec --remove unnecessary decimal places
		end
	end
	
	return math.log10(n)>=6 and (m>0 and "-" or "")..nn..""..(not isinf and p.illion({args={math.floor(ep/3-1)}}) or "") or f.args[1]
end

function p.illion(frame)
	local f=frame
	local n=f.args[1]~=nil and tonumber(f.args[1]) or 0 --illion number
	local u00=f.args[2]==nil and true or f.args[2] --use e00?
	local b0=(f.args[3]==nil and false or f.args[3]) and 1 or 0 --blank illion 0?
	local e00={"k","M","B"}
	local e0={"","U","D","T","Qa","Qi","Sx","Sp","O","N"}
	local e1={"","De","Vg","Tg","Qag","Qig","Sxg","Spg","Og","Ng"}
	local e2={"","Ce","Du","Tr","Qu","Qn","Se","St","Oc","No"}
	local e3={"Mi","Mc","Na","Pc","Fm","At","Zp","Yc","Xo","Vc","Mec","Duc","Trc","Ttc","Ptc","Hxc","Hpc","Otc","Enc","Ico","Meic","Duic","Tric","Ttic","Ptic","Hxic","Hpic","Otic","Enic","Trct","Metr","Dutr","Trtr","Tttr","Pttr","Hxtr","Hptr","Ottr","Entr","Ttri","Mettr","Duttr","Trttr","Ttttr","Ptttr","Hxttr","Hpttr","Otttr","Enttr","Pct","Mepc","Dupc","Trpc","Ttpc","Ptpc","Hxpc","Hppc","Otpc","Enpc","Hxct","Mehxc","Duhxc","Trhxc","Tthxc","Pthxc","Hxhxc","Hphxc","Othxc","Enhxc","Hpct","Mehpc","Duhpc","Trhpc","Tthpc","Pthpc","Hxhpc","Hphpc","Othpc","Enhpc","Otct","Meotc","Duotc","Trotc","Ttotc","Ptotc","Hxotc","Hpotc","Ototc","Enotc","Enct","Meenc","Duenc","Trenc","Ttenc","Ptenc","Hxenc","Hpenc","Otenc","Enenc","Hc","Mehc","Duhc"}
	local i=0
	local r=f.args[4]==nil and 0 or f.args[4]
	
	if n>=math.huge then
		return math.huge
	elseif n>=999 then
		i=10^(3*math.floor(math.log10(n)/3))
		return (math.floor(n/i)>1 and p.illion({args={math.floor(n/i),false}}) or "")..(math.floor(math.log10(n)/3)<=#e3 and e3[math.floor(math.log10(n)/3)] or "["..math.floor(math.log10(n)/3).."]")..(r<10 and p.illion({args={n%i,nil,true,r+1}}) or "")..(r==10 and "..." or "") --it can only go so far down, this limit is to prevent not enough memory errors
	else
		return n<b0 and "" or (u00 and n<#e00) and e00[1+n] or e0[1+(n%10)]..e1[1+(math.floor(n/10)%10)]..e2[1+(math.floor(n/100)%10)]
	end
end
return p
Cookies help us deliver our services. By using our services, you agree to our use of cookies.