Content deleted Content added
Improvements waiting in sandbox Tag: Reverted |
Add wordify Tag: Reverted |
||
Line 1:
-- This module is intended to replace the functionality of Template:Formatnum and related templates.
local
local p = {} -- Holds functions to be returned from #invoke, and functions to make available to other Lua modules.
--[[
Helper functions used to avoid redundant code.
]]
local function err(msg)
-- Generates wikitext error messages.
return mw.ustring.format('<strong class="error">Formatting error: %s</strong>', msg)
end
function p.main(frame)
Line 127 ⟶ 138:
return number
end
--[[
mod
Implements the division operator
Usage:
{{#invoke:Formatnum | wordify | x | y | round= | precision= }}
--]]
function p.wordify(frame)
local args = frame:getParent().args
if not args[1] and not args.number then
args = frame.args
end
local x = args[1]
local numsys = args.numsys
local prec = args.prec
local lk = args.lk
return p._wordify(x, numsys, prec, (lk == "on" and true or false))
end
function p._wordify(x, numsys, prec, lk)
if tonumber(x) then
if numsys == "usa" or numsys == nil or numsys == "" then
if x / 1E12 >= 1 then
return p.formatNum(mm._round(x / 1E12, prec), "en") .. " " .. (lk and "[[1,000,000,000,000|trillion]]" or "trillion")
elseif x / 1E9 >= 1 then
return p.formatNum(mm._round(x / 1E9, prec), "en") .. " " .. (lk and "[[1,000,000,000|billion]]]" or "billion")
elseif x / 1E6 >= 1 then
return p.formatNum(mm._round(x / 1E6, prec), "en") .. " million"
else
return p.formatNum(mm._round(x, prec), "en")
end
elseif numsys == "ind" then
if x / 1E12 >= 1 then
return p.formatNum(mm._round(x / 1E12, prec), "en") .. " " .. (lk and "[[lakh]] [[crore]]" or "lakh crore")
elseif x / 1E7 >= 1 then
return p.formatNum(mm._round(x / 1E7, prec), "en") .. " " .. (lk and "[[crore]]" or "crore")
elseif x / 1E5 >= 1 then
return p.formatNum(mm._round(x / 1E5, prec), "en") .. " " .. (lk and "[[lakh]]" or "lakh")
else
return p.formatNum(mm._round(x, prec), "en")
end
else
return err("number system not supported")
end
else
return err("Not a number: " .. x)
end
end
--[[
Helper function that interprets the input numerically. If the
input does not appear to be a number, attempts evaluating it as
a parser functions expression.
]]
function p._cleanNumber(number_string)
if type(number_string) == 'number' then
-- We were passed a number, so we don't need to do any processing.
return number_string, tostring(number_string)
elseif type(number_string) ~= 'string' or not number_string:find('%S') then
-- We were passed a non-string or a blank string, so exit.
return nil, nil;
end
-- Attempt basic conversion
local number = tonumber(number_string)
-- If failed, attempt to evaluate input as an expression
if number == nil then
local success, result = pcall(mw.ext.ParserFunctions.expr, number_string)
if success then
number = tonumber(result)
number_string = tostring(number)
else
number = nil
number_string = nil
end
else
number_string = number_string:match("^%s*(.-)%s*$") -- String is valid but may contain padding, clean it.
number_string = number_string:match("^%+(.*)$") or number_string -- Trim any leading + signs.
if number_string:find('^%-?0[xX]') then
-- Number is using 0xnnn notation to indicate base 16; use the number that Lua detected instead.
number_string = tostring(number)
end
end
return number, number_string
end
|