TextLabel automatically add commas to numbers

Need a local script that automatically adds commas to numbers like 1,000, 100,000, 1,000,000, 10,000,000 or 1,245, 481,193, 2,428,732 etc.

This is not the first time this has been asked in the devforums. This script can help you, just format the text with tonumber() and use the function.

-- script inside textlabel
function comma_value(amount)
	local formatted = amount
	while wait() do
		formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)","%1,%2")
		if (k==0) then
			break
		end
	end
	return formatted
end

script.Parent:GetPropertyChangedSignal("Text"):Connect(function()
	script.Parent.Text = comma_value(tonumber(script.Parent.Text))
end)

-- Example: comma_value(2428732) -- 2,428,732
1 Like

didnt work
30 crssssssssssssssssss

local function formatNumber(number, language)
	number = tostring(number)
	local comma, decimalPoint = language == "Germany" and "." or ",", language == "Germany" and "," or "."
	local decimal = string.match(number, "%.(%d+)")
	number = string.match(number, "(%d+)%.?")
	local format = string.gsub(number, "(%d)(%d%d%d)$", `%1{comma}%2`)
	while true do
		local temp = string.gsub(format, "(%d)(%d%d%d),", `%1{comma}%2{comma}`)
		if temp == format then break end
		format = temp
	end
	return `{format}{decimal and decimalPoint or ""}{decimal or ""}`
end

I know it’s quite confusing, but this function also works with formatting numbers in Germany, as well as decimal places too.

This is a modified version of another post.

didnt work
30charlimitssssssssss

That’s weird… it does work in my game however.

Maybe try using print(formatNumber(number)).

print(formatNumber(number))

where do I put this?