Help AbbreviatingNumbers

Function I wrote a while ago to do this:

Function @berezaa wrote to do this:

And some other options by @5uphi and @itzmerose_12:

My current favorite is the one by itzmerose_12 for its simplicity:

local format = string.format
local function Abbreviate(x: number, decimals: number?)
	if decimals == nil then decimals = 0 end
	
	local fmt = "%." .. decimals .. "f%s"	
	if x >= 1e69 then return format(fmt, x / 1e69, "Gp")
	elseif x >= 1e66 then return format(fmt, x / 1e66, "G")
	elseif x >= 1e63 then return format(fmt, x / 1e63, "V")
	elseif x >= 1e60 then return format(fmt, x / 1e60, "Ncd")
	elseif x >= 1e57 then return format(fmt, x / 1e57, "Ocd")
	elseif x >= 1e54 then return format(fmt, x / 1e54, "Spd")
	elseif x >= 1e51 then return format(fmt, x / 1e51, "Sid")
	elseif x >= 1e48 then return format(fmt, x / 1e48, "Qid")
	elseif x >= 1e45 then return format(fmt, x / 1e45, "Qad")
	elseif x >= 1e42 then return format(fmt, x / 1e42, "Trd")
	elseif x >= 1e39 then return format(fmt, x / 1e39, "Dnd")
	elseif x >= 1e36 then return format(fmt, x / 1e36, "Und")
	elseif x >= 1e33 then return format(fmt, x / 1e33, "D")
	elseif x >= 1e30 then return format(fmt, x / 1e30, "N")
	elseif x >= 1e27 then return format(fmt, x / 1e27, "O")
	elseif x >= 1e24 then return format(fmt, x / 1e24, "Sp")
	elseif x >= 1e21 then return format(fmt, x / 1e21, "s")
	elseif x >= 1e18 then return format(fmt, x / 1e18, "Qi")
	elseif x >= 1e15 then return format(fmt, x / 1e15, "Qa")
	elseif x >= 1e12 then return format(fmt, x / 1e12, "T")
	elseif x >= 1e9 then return format(fmt, x / 1e9, "B")
	elseif x >= 1e6 then return format(fmt, x / 1e6, "M")
	elseif x >= 1e3 then return format(fmt, x / 1e3, "k")
	else return format(fmt, x, "")
	end
end
3 Likes