Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Formats numbers with thousand separators. Non-numeric input is returned unchanged, so the module is safe to use on mixed content (e.g., empty text, labels passed through in error).

Examples

Code Result
{{#invoke:Number|main|value=1500}} 1,500
{{#invoke:Number|main|value=1500.50}} 1,500.50
{{#invoke:Number|main|value=-1500}} -1,500
{{#invoke:Number|main|value=abc}} abc

The value parameter is required (named). Leading and trailing whitespace is trimmed.

Notes

  • Trailing zeros in the decimal portion are preserved: 1500.50 formats as 1,500.50, not 1,500.5.
  • Input like 1500. (trailing dot, no digits) is treated as invalid and returned unchanged.

local Number = {}

local lang = mw.getContentLanguage()

--- Format a number with thousand separators.
--- Non-numeric input is returned unchanged.
---
--- @param value string|number
--- @return string
local function format(value)
    local str = tostring(value)
    local sign, integerPart, decimalPart = str:match("^(%-?)(%d+)(%.?%d*)$")
    if not integerPart or decimalPart == "." then return str end
    return sign .. lang:formatNum(tonumber(integerPart)) .. decimalPart
end

--- Entry point: {{#invoke:Number|main|value=1500}}
function Number.main(frame)
    return format(mw.text.trim(frame.args.value or ""))
end

return Number