Adding commas to number? e.g 1000 to 1,000?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?

Add commas to numbers like: 1000, 100000, 1000000 etc

  1. What is the issue?

I’m not sure how to do this

  1. What solutions have you tried so far?

Developer Hub, YouTube.

If anyone could save me I love it

1 Like

There’s a few threads about this. Try seeing if any of them answer your question. Please make sure to search more sources before posting threads.

https://devforum.roblox.com/search?context=topic&context_id=808398&q=commas%20number&skip_context=true

1 Like

I did look in them but I found no efficient answers

4 Likes
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

print(comma_value(12000)) -- 12,000
2 Likes

Could you explain what do you mean by “efficient” and how are these inefficient?

1 Like

That is not efficient is it?

1 Like

Like it is not working at its best or is not the best way of doing it

1 Like

I would replace your while wait() do with just while true do.

Or swap the whole loop for

do
    formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)","%1,%2")
while k != 0

Also you forgot to forward declare k.

1 Like

You asked for a method to add commas to numbers, you did not ask for the most efficient way to do this.

1 Like

Here OP I googled it for you:

2 Likes

I did ask for most efficient

1 Like

You should use Translator:FormatByKey. It supports multiple locales, not just US English.

local LocalizationService = game:GetService("LocalizationService")
local Players = game:GetService("Players")

local player = Players.LocalPlayer
local translator = LocalizationService:GetTranslatorForPlayerAsync(player)
-- Requires your localization table have an entry with the key "yourgame.foo.bar" with a value like `Coins: {Number:num}`
local outString = translator:FormatByKey("yourgame.foo.bar", {
    Number = 1234567.8
})
--> US player sees: 1,234,567.8
--> UK player sees: 1 234 567.8
--> German player sees: 1.234.567,8

Generally for tasks like this, correctness matters much more than being efficient. Efficiency only matters when you are doing something at a high rate, such as a hundred times per frame. Updating a number in a TextLabel is not one of those tasks.

27 Likes

I don’t… I don’t really get that though can you explain it like laymens terms?

1 Like

There is an article that he provided in his post.

Anyways, here is a line-by-line explanation

1.) Retrieve the localization service (offers translational methods)
2.) Retrieve the players service
3.)
4.) Access the player for which the script is running
5.) Retrieve translator for the local player (this is what is used to translate the string/number)
6.) Translate the number/string. This is what actually formats your number.

Disclaimer: I don’t have experience with the translator service.

1 Like

What do I replace “yourgame.foo.bar” with?

1 Like

Thats way in not efficient ???

Did you measure it? I’m not sure you have a clear definition for what efficient means. @Tiffblocks has the best roblox-specific answer, anyways.

1 Like

Yes but what do I replace "yourgame.foo.bar” with???

Replace this with the Key value to look up and translate

Find out more about FormatByKey here

Replace it with your value, it’s just an example. Please be patient as we try to help you.