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
Could you explain what do you mean by “efficient” and how are these inefficient?
That is not efficient is it?
Like it is not working at its best or is not the best way of doing it
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
.
You asked for a method to add commas to numbers, you did not ask for the most efficient way to do this.
Here OP I googled it for you:
I did ask for most efficient
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.
I don’t… I don’t really get that though can you explain it like laymens terms?
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.
What do I replace “yourgame.foo.bar” with?
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.
Yes but what do I replace "yourgame.foo.bar” with???
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
What isn’t efficient about any of these answers???