Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to add comma to a number value ?

I just want to players see this in there cash Gui (EXEMPLE : 1,253,453)

so when they play they will not see as 1253453

the script that i made d'ont wanna execute it just not showing me comma when i add 1000 in the number value

game.Players.PlayerAdded:Connect(function(plr)
	local stats = Instance.new("Model", plr)
	stats.Name = "Leaderstats"
	local cash = Instance.new("NumberValue", stats)
	cash.Value = 0
	cash.Name = "Cash"
	
return #cash % 3 == 0 and cash:reverse():gsub("(%d%d%d)", "%1,"):reverse():sub(2) or cash:reverse():gsub("(%d%d%d)", "%1,"):reverse()

end)

3 comments

0

I d'ont understand this , you know i am not a programmer student

0

you could use string.len() (gets length) and just check every time the cash length increase(1000 to 10000) then just add in the comma's where you know they'll be. Not the best way but is the simplest.

1 answer

You have the right idea but a bit off track...

You where correct on using gsub and such, but you where just a tiny bit off track. Heres the correct function to convert an int. :)

function format_int(number)
	local i, j, minus, int, fraction = tostring(number):find('([-]?)(%d+)([.]?%d*)')
	int = int:reverse():gsub("(%d%d%d)", "%1,")
	return minus .. int:reverse():gsub("^,", "") .. fraction
end

print(format_int(1234))
print(format_int(10000000))
print(format_int(249949))
print(format_int(-1234))

--[[ CONSOLE
	1,234
	10,000,000
	249,949
	-1,234
]]--

This discussion is preserved as it was. New answers, comments, and votes are closed.