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

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.

I still dont understand what you are saying man

You know what? I sort of take back my suggestion to use @Tiffblocks’s solution.

Is it the best solution? Yes, unambiguously, you should use it for serious projects that you want to localize.

ROBLOX provides really cool and powerful tools to translate your game. Here’s an introduction to how localization works, and here is an introduction to how to bridge the gap between your game’s localization table, and your script showing a number.

However, that’s a lot of API for not a lot of benefit IMO. It’s not like the rest of your game is localized, anyways. Just (and some people would probably yell at me for suggesting this) use a stupid american-english-only function for this specific case.

Who cares? Are you planning on a lot of brits playing your game and worried about them being confused that there’s commas instead of periods? You’re not building a AAA game here.


I stole this from the link I posted earlier. Just use it and move on with your game.

function format_int(number)

  local i, j, minus, int, fraction = tostring(number):find('([-]?)(%d+)([.]?%d*)')

  -- reverse the int-string and append a comma to all blocks of 3 digits
  int = int:reverse():gsub("(%d%d%d)", "%1,")

  -- reverse the int-string back remove an optional comma and put the 
  -- optional minus and fractional part back
  return minus .. int:reverse():gsub("^,", "") .. fraction
end

print(format_int(1000000)) -- prints 1,000,000
print(format_int(1000000.5555)) -- prints 1,000,000.5555
9 Likes

What isn’t efficient about any of these answers???

7 Likes