Jump to content
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.

-- Number formatting helpers for templates and other modules.
-- See Module:Number/doc for usage and behavior notes.

local Number = {}

--- Trim whitespace and convert nil to an empty string.
local function normalizeInput(value)
    if value == nil then return "" end
    return mw.text.trim(tostring(value))
end

--- Format a number with the wiki's locale-correct grouping separator.
--- Trailing zeros, leading zeros, and full input precision are preserved.
--- Non-numeric input is returned unchanged.
function Number.format(value)
    local str = normalizeInput(value)
    if not tonumber(str) then return str end
    local frame = mw.getCurrentFrame()
    return frame:callParserFunction('formatnum', str)
end

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

return Number