--[[
	Stringifies a number and places a separator between thousands.


	```lua
	separateThousands(1000000) -- 1,000,000
	separateThousands(12345678, ".") -- 12.345.678
	```

	This function is called when setting the text for the PrimaryButton
	component when in Purchase mode; the text reflects the price in Robux.

	@within Modules
]]


local function separateThousands(n: number, sep: string?): string
	sep = sep or ","

	local str = tostring(n)

	-- Reverse to apply commas from right-to-left of the original number.
	str = str:reverse():gsub("%d%d%d", "%1,")

	-- Reverse back to normal and trim off any leading commas. The trim is
	-- needed when the number of digits is divisible by 3.
	str = str:reverse():gsub("^,", "")

	return str
end

return separateThousands
