require('strict')
local p = local lang = mw.language.getContentLanguage; -- language object for this wikilocal presentation =; -- table of tables that contain currency presentation datalocal properties;
----------------------------< I S _ S E T >------------------------------------------------------------------
Whether variable is set or not. A variable is set when it is not nil and not empty.
local function is_set(var) return not (var
);end
----------------------------< M A K E _ S H O R T _ F O R M _ N A M E >-------------------------------------
Assembles value and symbol according to the order specified in the properties table for this currency code
local function make_short_form_name (amount, code, linked, passthrough) local symbol; local position = properties[code].position;
if linked then symbol = string.format ('%s', properties[code].page, properties[code].symbol); -- make wikilink of page and symbol else symbol = properties[code].symbol; end
if not passthrough then amount = lang:formatNum (tonumber(amount)); -- add appropriate comma separators end amount = amount:gsub ('^%-', '−'); -- replace the hyphen with unicode minus
if 'b'
position then -- spaced before the amount return string.format ('%s %s', symbol, amount); elseif 'a'
position then -- spaced after the amount return string.format ('%s %s', amount, symbol); elseif 'd'
----------------------------< M A K E _ N A M E >----------------------------------------------------------
Make a wikilink from the currency's article title and its plural (if provided). If linked is false, returns onlythe article title (unlinked)
local function make_name (linked, page, plural) if not linked then if not is_set (plural) then return page; -- just the page elseif 's'
plural then -- if the simple plural form return string.format ('%ss', page); else return string.format ('%s', page, plural); -- must be the complex plural form (pounds sterling v. dollars) end endend
----------------------------< M A K E _ L O N G _ F O R M _ N A M E >---------------------------------------
assembles a long-form currency name from amount and name from the properties tables; plural for all values not equal to 1
local function make_long_form_name (amount, code, linked, passthrough) local name, formatted; if not is_set (properties[code].page) then return '
– definition missing page (help)'; endif not passthrough then amount = tonumber (amount); -- make sure it's a number end
if 1
return string.format ('%s %s', formatted, name); -- put it all togetherend
----------------------------< R E N D E R _ C U R R E N C Y >------------------------------------------------
Renders currency amount with symbol or long-form name.
Also, entry point for other modules. Assumes that parameters have been vetted; amount is a number, code is uppercase string, long_form is boolean; all are required.
local function render_currency (amount, code, long_form, linked, fmt, passthrough) local name; local result;
presentation = mw.loadData ('Module:Currency/Presentation'); -- get presentation data
if presentation.currency_properties[code] then -- if code is an iso 4217 code properties = presentation.currency_properties; elseif presentation.code_translation[code] then -- not iso 4217 but can be translated code = presentation.code_translation[code]; -- then translate properties = presentation.currency_properties; elseif presentation.non_standard_properties[code] then -- last chance, is it a non-standard code? properties = presentation.non_standard_properties; else return '
– invalid code (help)'; endif long_form then result = make_long_form_name (amount, code, linked, passthrough); -- else result = make_short_form_name (amount, code, linked, passthrough); end if 'none'
fmt then -- use narrow gaps result = result:gsub ('(%d%d?%d?),', '
%1'); -- replace comma seperators elseif fmt and 'commas' ~= fmt then -- if not commas (the default) then error message return ' – invalid format (help)'; endreturn result; -- doneend
--
local function parse_formatted_number (amount) local count; local parts = ; local digits = ; local decimals; local sign = ; local _; if amount:find ('[^%-−%d%.,]') then -- anything but sign, digits, decimal points, or commas return nil; end amount = amount:gsub ('−', '-'); -- replace unicode minus with hyphen _, count = amount:gsub('%.', ) -- count the number of decimal point characters if 1 < count then return nil; -- too many dots end
_, count = amount:gsub(',', ) -- count the number of grouping characters if 0
parts = mw.text.split (amount, '.', true); -- split amount into digits and decimals decimals = table.remove (parts, 2) or ; -- if there was a decimal portion, remove from the table and save it
digits = mw.text.split (parts[1], ',') -- split amount into groups for i, v in ipairs (digits) do -- loop through the groups if 1
return sign .. table.concat (digits) .. '.' .. decimals; -- reassemble without commas and returnend
----------------------------< C O N V E R T _ S T R I N G _ T O _ N U M E R I C >------------------------------------------------
Converts quantified number/string combinations to a number e.g. 1 thousand to 1000.
local function convert_string_to_numeric (amount) local quantifiers = ;
local n, q = amount:match ('([%-−]?[%d%.,]+)%s*(%a+)$'); -- see if there is a quantifier following a number; zero or more space characters if nil
n then return amount end; -- if not
if nil
--amount=, |Amount= : digits and decimal points only positional (2nd), |type=, |Type= : code that identifies the currency |first= : uses currency name instead of symbol
local function currency (frame) local args = require('Module:Arguments').getArgs (frame);
local amount, code; local long_form = false; local linked = true; local passthrough = false;
if not is_set (args[1]) then return '
– invalid amount (help)'; end -- amount = lang:parseFormattedNumber(args[1]); -- if args[1] can't be converted to a number then error (this just strips grouping characters)-- if args[1]:find ('[^%d%.]') or not amount then -- non-digit characters or more than one decimal point (because lag:parse... is broken)-- return ' – invalid amount (help)';-- end-- This allows us to use while actually following as regards "billion", "million", "M", "bn", etc. if not (args['passthrough']
return render_currency (amount, code, long_form, linked, args['fmt'], (args['passthrough']
return