How would i make a large number have commas?

So what i’m trying to do is get a number like 10000 to look like 10,000.
It seems pretty simple but i have no idea how to do it.
Maybe just code it to do that?

All help is appreciated.

8 Likes

Take a look at this. The page has a list of functions that do stuff like this. Here is the function it lists for adding commas to functions.

function comma_value(amount)
  local formatted = amount
  while true do  
    formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
    if (k==0) then
      break
    end
  end
  return formatted
end
72 Likes

I think this belongs more in the scripting support category.

3 Likes
1 Like

I tried this function but there seems to be an error with k. The function didn’t work.

1 Like

Uh seems to work fine for me. Here is an example with the correct result.

function comma_value(amount)
	local formatted = amount
	while true do  
		formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
		if (k==0) then
			break
		end
	end
	return formatted
end

local NumberWithCommas = 1000000

print(comma_value(NumberWithCommas))
9 Likes

Hey, you able to explain how the function works? I’m not sure what the “^(-?%d+)(%d%d%d)” part means

2 Likes

I don’t know too much about it, I just took the code from the guy who made the function and made it work. I am about where you are with it not knowing exactly what it does. I know it does have to do with String Pattern, so I would say maybe look at the wiki for deep info about it. I tried looking at it so I could explain it but it would take me a bit to fully understand and be able to explain it to someone else. Here is the Wiki for String Patterns Strings | Documentation - Roblox Creator Hub

Thank’s to both of you for this! I’m going to make a wallet system for my friends game that I’m helping to develop and I needed a Gui that displays the players coins but with commas as a replacement of the leaderboard on the players list.

Hi Dev,

We use an “IntValue” to detect values ​​that we want, but when we want real numbers with numbers and much more, it is very difficult to find, so I made a basic script just to show how to use commas in these types of cases

When the value is more than 3 digits it will put a virulgas after the 1 or 2 number, so this script below:

Add a TextLabel, and then a LocalScript and add this script inside

local RunService = game:GetService("RunService")
local LocalPlayer = game:GetService("Players").LocalPlayer
local Cash = LocalPlayer.Cash --Detect "IntValue" (Real Money)
local Text = script.Parent 

local IsType = (function (T)
	T = tostring(T)
	return T:reverse():gsub("%d%d%d", "%1,"):reverse():gsub("^,", "")
end)

RunService.RenderStepped:Connect(function()
	Text.Text = "$: " .. IsType(Cash.value) -- Texto % Value
end)```
3 Likes

What does that do? specifically the return T:reverse():gsub("%d%d%d", "%1, "):reverse():gsub("^,", "")

local IsType = (function (T)
    -- converts 1234567890 to "1234567890"
	T = tostring(T)

	return T
        -- converts "123456789" to "987654321"
        :reverse()

        -- replaces every 3 digits with the captured string (the 3 digits) + a comma
        -- so "321" becomes "321,", "654321" "654,321," and "987654321" "987,654,321,"
        :gsub("%d%d%d", "%1,")

        -- converts "987,654,321," to ",123,456,789"
        :reverse()

        -- replaces the comma at the start of the string with nothing
        -- ",123,456,789" to "123,456,789"
        :gsub("^,", "")
end)
12 Likes