Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to format numbers? Welp

Asked by
NewGPU 36
6 years ago

note: edited this to explain a bit better

I did this ages ago but I forgot & I've googled and found nothing.

I want my screengui text numbers like this picture below

https://imgur.com/a/DejEK (looks like that but isnt going to be used for leaderboard)

I just want to format my numbers to "1,234" instead of "1234" and use it in a gui with a textlabel. Example:

script.Parent.Text = player:WaitForChild("MyNumbers").Value player:WaitForChild("MyNumbers").Changed:Connect(function() 
script.Parent.Text = player:WaitForChild("MyNumbers").Value end) 

instead of having it displayed as 1234 (which this does) i want to display it as 1,234 (which i cant figure out) if that was my value.

2 answers

Log in to vote
0
Answered by 6 years ago
local function AddCommas(Number)
    if typeof(Number) == "number" then Number = tostring(Number) end
    local Index = -1
    while Index ~= 0 do
        Number, Index = Number:gsub("^(-?%d+)(%d%d%d)", "%1,%2")
    end
    return Number
end
print(AddCommas(1234))
Ad
Log in to vote
0
Answered by 6 years ago
function comma(amt)
    local formatted = amt

    while wait() do  
        formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')

        if k == 0 then
            break
        end
    end

    return formatted
end

Answer this question