How to abbreviate large numbers?

I’m trying to abbreviate numbers in my game (like 10k, 10m) because the numbers will go off the screen if they are too big. I can’t find any solution to this Anyone able to help?

1 Like

I have a script, hope it helps!

-- if it is a Gui Then
local TextLabel = pathwayto.TextLabel
local abbreviations = {
	N = 10^30;
	O = 10^27;
	Sp = 10^24;
	Sx = 10^21;
	Qn = 10^18;
	Qd = 10^15;
	T = 10^12;
	B = 10^9;
	M = 10^6;
	K = 10^3
}


function abbreviateNums(number)
	
	
	local abbreviatedNum = number
	local abbreviationChosen = 0
	
	
	for abbreviation, num in pairs(abbreviations) do
		
		if number >= num and num > abbreviationChosen then
			
			local shortNum = number / num
			local intNum = math.floor(shortNum)
				
			abbreviatedNum = tostring(intNum) .. abbreviation .. "+"
			abbreviationChosen = num
		end
	end
	
	return abbreviatedNum
end

game:GetService("RunService").RenderStepped:Connect(function()
       TextLabel.Text = abbreviateNums(TextLabel.Text)
end)
2 Likes

You can use math.floor(math.log(x,10^3)) to give you an index of the abbreviation instead of iterating through all of the abbreviations.

2 Likes

I get the error
Players.Towren.PlayerGui.StatValueGUI.ShardsValue.ShardValue.LocalScript:26: attempt to compare number <= string

Can you give me the script you’re currently editing?

-- if it is a Gui Then
local TextLabel = script.Parent
local abbreviations = {
	N = 10^30;
	O = 10^27;
	Sp = 10^24;
	Sx = 10^21;
	Qn = 10^18;
	Qd = 10^15;
	T = 10^12;
	B = 10^9;
	M = 10^6;
	K = 10^3
}


function abbreviateNums(number)


	local abbreviatedNum = number
	local abbreviationChosen = 0


	for abbreviation, num in pairs(abbreviations) do

		if number >= num and num > abbreviationChosen then

			local shortNum = number / num
			local intNum = math.floor(shortNum)

			abbreviatedNum = tostring(intNum) .. abbreviation .. "+"
			abbreviationChosen = num
		end
	end

	return abbreviatedNum
end

while wait() do
	TextLabel.Text = game.Players.LocalPlayer.leaderstats.Shards.Value
	TextLabel.Text = abbreviateNums(TextLabel.Text)
end
-- if it is a Gui Then
local Value = game.Players.LocalPlayer.leaderstats.Shards
local TextLabel = script.Parent
local abbreviations = {
	N = 10^30;
	O = 10^27;
	Sp = 10^24;
	Sx = 10^21;
	Qn = 10^18;
	Qd = 10^15;
	T = 10^12;
	B = 10^9;
	M = 10^6;
	K = 10^3
}


function abbreviateNums(number)


	local abbreviatedNum = number
	local abbreviationChosen = 0


	for abbreviation, num in pairs(abbreviations) do

		if number >= num and num > abbreviationChosen then

			local shortNum = number / num
			local intNum = math.floor(shortNum)

			abbreviatedNum = tostring(intNum) .. abbreviation .. "+"
			abbreviationChosen = num
		end
	end

	return abbreviatedNum
end

TextLabel.Text = abbreviateNums(Value.Value)

Value.Changed:Connect(function()
      TextLabel.Text = abbreviateNums(Value.Value)
end)
6 Likes
local abbreviations = {"K","M","B","T","Qd","Qn","Sx","Sp","O","N"}

function abbreviateNumber(number)
	local abbreviationIndex = math.floor(math.log(number,1000))
	local abbreviation = abbreviations[abbreviationIndex]

	if abbreviation then
		local shortNum = number/(1000^abbreviationIndex)
		local intNum = math.floor(shortNum)
		local str = intNum .. abbreviation
		if intNum < shortNum then
			str = str .. "+"
		end
		return str
	else
		return tostring(number)
	end
end
2 Likes

Actually, Miners Haven was open sourced a while back, and a useful module exactly for this was made.

I believe line 113 is the beginning of the code you are looking for.

Code if you don't want to go to github.
local function shorten(Input)
	local Negative = Input < 0
	Input = math.abs(Input)

	local Paired = false
	for i,v in pairs(MoneyLib.Suffixes) do
		if not (Input >= 10^(3*i)) then
			Input = Input / 10^(3*(i-1))
			local isComplex = (string.find(tostring(Input),".") and string.sub(tostring(Input),4,4) ~= ".")
			Input = string.sub(tostring(Input),1,(isComplex and 4) or 3) .. (MoneyLib.Suffixes[i-1] or "")
			Paired = true
			break;
		end
	end
	if not Paired then
		local Rounded = math.floor(Input)
		Input = tostring(Rounded)
	end

	if Negative then
		return "-"..Input
	end
	return Input
end
1 Like