Help AbbreviatingNumbers

Can anyone help me Abbreviate Numbers large numbers?

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

Thank you for all that but one last thing is that I don’t really understand what most of that does can you help me on that by explaining it a bit?

Which parts specifically are you referring to?

Sorry for the late response the parts in particular is in the function you said you wrote a while ago, I just don’t understand what all of the math does to get the finale result

Basically, math.log10(x) tells you “how many times do you have to multiply 10 to get x”

So for 1000 you get 3 (because 10*10*10)

For like 7000 you get 3.84, which still rounds down (math.floor) to 3

So you can use that to get the number of digits (floor(log10(x)) + 1)

Then there’s math.pow(10, x) which does the opposite: multiply 10 by itself x times. That’s useful because you can use it to shift the decimal around by x places by multiplying or dividing.

For example 555555 / math.pow(10, 3) → 555555 / 1000 → 555.555

You can use % with the same math.pow to get “just the bottom digits”: for example 555555 % math.pow(10, 2) → 555555 % 100 → 55

So you can combine those techniques to shuffle decimals around and figure out what abbreviation you need

1 Like

After reading this I have a much better understanding of what everything does thanks

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.