How to abbreviate numbers

Is there a way that I can abbreviate a player’s coins (1000 - 1k ect) like a simulator format? This is my local script within a text lable:

while wait() do
	local player = game:GetService("Players").LocalPlayer
	script.Parent.Text = "$"..player:WaitForChild("leaderstats").Candy.Value
end

Thanks for your time :slight_smile:

try this function.

function aberviate(num)
	if num < 1000 then
		return num
	else
		local number = math.floor(num)
		local abreviations = {
			[3] = "K",
			[6] = "M",
			[9] = "B",
			[12] = "T",
			[15] = "Qa"
		}
		local show = #tostring(number) % 3
		if show == 0 then
			show = 3
		end
		local trueLength = #tostring(number) - show
		if abreviations[trueLength] then
			number = tostring(number)
			return string.sub(number,1,show)..abreviations[trueLength]
		else
			return num
		end
	end
end

This was a quick function I whipped up. It can be infinity added upon for larger numbers. (Does not support decimals or negative numbers yet)

I recommend that you also make your code more efficient. Try this out:

local plr = game:GetService("Players").LocalPlayer

local leaderstats = plr:WaitForChild("leaderstats"):WaitForChild("Candy")

function aberviate(num)
	if num < 1000 then
		return num
	else
		local number = math.floor(num)
		local abreviations = {
			[3] = "K",
			[6] = "M",
			[9] = "B",
			[12] = "T",
			[15] = "Qa"
		}
		local show = #tostring(number) % 3
		if show == 0 then
			show = 3
		end
		local trueLength = #tostring(number) - show
		if abreviations[trueLength] then
			number = tostring(number)
			return string.sub(number,1,show)..abreviations[trueLength]
		else
			return num
		end
	end
end

leaderstats:GetPropertyChangedSignal("Value"):Connect(function()
	script.Parent.Text = "$" .. aberviate(leaderstats.Value)
end)

Is there a way that I can make it say 1.1k when the value is 1100 ect?

yes. Completely delete the old function and replace it with this. Do you see the decAcc variable? you can set that to how many decimals you want.

function aberviate(num)
	local decAcc = 2
	if num < 1000 then
		return num
	else
		local number = math.floor(num)
		local abreviations = {
			[3] = "K",
			[6] = "M",
			[9] = "B",
			[12] = "T",
			[15] = "Qa"
		}
		local show = #tostring(number) % 3
		if show == 0 then
			show = 3
		end
		local trueLength = #tostring(number) - show
		if abreviations[trueLength] then
			number = tostring(number)
			return string.sub(number,1,show)..".".. string.sub(number,show + 1,show+decAcc) ..   abreviations[trueLength]
		else
			return num
		end
	end
end
2 Likes

I have 1 last problem, because the text value only changes when the cash value changes, when I load into the game the text says “$ Cash” until i click to change the value, how can i fix this?

Try this:

local plr = game:GetService("Players").LocalPlayer

local leaderstats = plr:WaitForChild("leaderstats"):WaitForChild("Candy")

function aberviate(num)
	local decimalAcc = 2
	if num < 1000 then
		return num
	else
		local number = math.floor(num)
		local abreviations = {
			[3] = "K",
			[6] = "M",
			[9] = "B",
			[12] = "T",
			[15] = "Qa"
		}
		local show = #tostring(number) % 3
		if show == 0 then
			show = 3
		end
		local trueLength = #tostring(number) - show
		if abreviations[trueLength] then
			number = tostring(number)
			return string.sub(number,1,show).. string.sub(show,) ..   abreviations[trueLength]
		else
			return num
		end
	end
end

script.Parent.Text = "$0"

leaderstats:GetPropertyChangedSignal("Value"):Connect(function()
	script.Parent.Text = "$" .. aberviate(leaderstats.Value)
end)
1 Like