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
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.
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.