Small abbreating number module

A little modulescript I made that abbreates numbers.

I know there’s really easy the find this on the internet and most of the people with basic coding skills can do this themself, but I’ll will post this anyway in hopes this will be useful for someone.

--//IMPORTANT\\-- THIS IS A MODULESCRIPT AND WILL ONLY WORK IF YOU "CALL" IT FROM OTHER SCRIPTS
local AbbreateNumber = {}

function AbbreateNumber.Calculate(Number)
	local Digits = { --Amounts of zeros
		3,
		6,
		9,
		12,
		15,
	}
	local Letters = {
		"K",
		"M",
		"B",
		"T",
		"Qa",
	}
	
	local Letter

	local DivideNumber

	local Cal1 = math.floor(math.log10(Number)) --Some math

	if Cal1 > 2 then

		for i, v in pairs(Digits) do
			if Cal1 == v or Cal1-1 == v or Cal1-2 == v then
				Letter = Letters[i]
				DivideNumber = 10^v
				break
			else
				Letter = Letters[5]
				DivideNumber = 10^15
			end
		end
	

		local Cal2 = math.floor((Number/DivideNumber) * 100)/100 --"100" = amount of decimals, 10 = 1, 100 = 2, 1000 = 3 etc

		local NewNumber = tostring(Cal2)..Letter
		
		return NewNumber --Returns a string, for example 10.5M
		
	else
		return Number --Returns a number (no letter, for example 582) You can of course change by using tostring()
	end
end


return AbbreateNumber
1 Like