Skip to content
The Chat Archive
gpt-4-0314· OpenAI WildChat-4.8M (AI2)

fivem lua I've got a variable containing numbers eg price = 100000 how could i make it…

  1. User

    fivem lua I've got a variable containing numbers

    eg

    price = 100000

    how could i make it so it looks like 100,000?

  2. Assistant

    You can create a function that formats the number using string manipulation. Here's an example:

    function formatNumber(n)
        local formatted = tostring(n)
        local k
    
        while true do  
            formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
            if (k == 0) then
                break
            end
        end
        return formatted
    end
    
    price = 100000
    formattedPrice = formatNumber(price)
    print(formattedPrice)  -- Output: 100,000
    

    This function uses a loop to insert the commas in the correct positions. You can then call this function with your number to get the formatted string with commas.