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

Okay so I noticed my last script was trash, so I made a new one, which is much more efficient - also using meta tables, which I learnt recently.

Small-abbreating-number-module 2.0:

local AbbreateNumber = {}

function AbbreateNumber.Calculate(Number)
	local Number = Number or 1 -- You can change the "1" to whatever you want

	local Letters = {
		"K", 
		"M", 
		"B", 
		"T", 
		"Qa", 
		"Qi", 
		"Sx", 
		"Sp", 
		"Oc", 
		"No", 
		"Dc", 
		"Ud", 
		"Dd" 
	}
	
	local MetaTable = setmetatable(Letters, {	
		__index = function()
			local LetterAmount = #Letters		
			return tostring(math.floor(Number/10^(LetterAmount*3)) .. Letters[LetterAmount])
		end,	
	})
	
	local Cal1 = math.floor(math.floor(math.log10(Number))/3)

	if Cal1 < 1 then return Number end
	
	local Letter = Letters[Cal1]
	
	if string.find(Letter, "%d") then return Letter end

	local Cal2 = math.floor((Number/10^(Cal1*3)) * 100)/100

	local NewNumber = tostring(Cal2)..Letter
		
	return NewNumber
end

return AbbreateNumber