Module:NumberFormatter

From Idle Clans wiki
Revision as of 10:00, 24 May 2024 by Uraxys (talk | contribs) (Created page with "local p = {} ---Formats a number by adding thousand separators. ---Example: 20000 -> 20,000 function p.formatNumber(frame) local num = tonumber(frame.args[1]) -- Make sure we're working with a number. if not num then return "Error: '" .. frame.args[1] .. "' is not a number." end -- Format the number. local formatted = tostring(num) while true do formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2') if k...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:NumberFormatter/doc

local p = {}

---Formats a number by adding thousand separators.
---Example: 20000 -> 20,000
function p.formatNumber(frame)
    local num = tonumber(frame.args[1])

    -- Make sure we're working with a number.
    if not num then
        return "Error: '" .. frame.args[1] .. "' is not a number."
    end

    -- Format the number.
    local formatted = tostring(num)
    while true do
        formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
        if k == 0 then
            break
        end
    end

    -- Return the formatted number.
    return formatted
end

return p