Module:Currency
Appearance
![]() | This Lua module is used on approximately 4,600 pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them. |
Related pages |
---|
This code is invoked from {{Currency}}
. All of the template parameters are passed in the module's frame.
{{#invoke:Currency|currency|<amount>|<code>|<first>|<linked>|<passthrough>}}
See the template code for a description of the parameters.
Other modules may use this code. The entry point for other modules is _render_currency (amount, code, long_form, linked)
. See the function render_currency()
for detail.
The data file Module:Currency/Presentation holds required currency presentation characteristics.
require('Module:No globals')
local getArgs = require('Module:Arguments').getArgs
local p = {}
--[=[-------------------------< C U R R E N C Y P R O P E R T I E S >----------------------------------------
this table is created from Template:Currency/Page, Template:Currency/Position, and Template:Currency/Symbol. The
individual currency codes are taken from the table at [[ISO 4217]]. The source templates have other codes not
conforming to 4217; those codes are not included here but are listed in the translation table or in the non-standard
table.
]=]
local currency_properties = {
['AED'] = {page = 'United Arab Emirates dirham', plural = 's', position = 'bs', symbol = 'AD'},
['AFN'] = {page = 'Afghan Afghani', plural = 's', position = 'b', symbol = '؋'},
['ALL'] = {page = 'Albanian lek', plural = 's', position = 'b', symbol = 'L'},
['AMD'] = {page = 'Armenian dram', plural = 's', position = 'bs', symbol = 'դր.'},
['ANG'] = {page = 'Netherlands Antillean guilder', plural = 's', position = 'b', symbol = 'NAƒ'},
['AOA'] = {page = 'Angolan kwanzakwanzas', plural = 's', position = 'bs', symbol = 'Kz'},
['ARS'] = {page = 'Argentine peso', plural = 's', position = 'b', symbol = 'AR$'},
['AUD'] = {page = 'Australian dollar', plural = 's', position = 'b', symbol = 'A$'},
['AWG'] = {page = 'Aruban guilder', plural = 's', position = 'bs', symbol = 'Afl.'},
['AZN'] = {page = 'Azerbaijani manat', plural = 's', position = 'b', symbol = 'man.'},
['BAM'] = {page = 'Bosnia and Herzegovina konvertibilna marka', plural = 's', position = 'bs', symbol = 'KM'},
['BBD'] = {page = 'Barbados dollar', plural = 's', position = 'bs', symbol = 'Bds$'},
['BDT'] = {page = 'Bangladeshi taka', plural = 's', position = 'b', symbol = '৳'},
['BGN'] = {page = 'Bulgarian lev', plural = 's', position = 'bs', symbol = 'лв'},
['BHD'] = {page = 'Bahraini dinar', plural = 's', position = 'bs', symbol = 'BD'},
['BIF'] = {page = 'Burundian franc', plural = 's', position = 'bs', symbol = 'FBu'},
['BMD'] = {page = 'Bermudian dollar', plural = 's', position = 'b', symbol = 'BD$'},
['BND'] = {page = 'Brunei dollar', plural = 's', position = 'b', symbol = 'B$'},
['BOB'] = {page = 'Boliviano', plural = 's', position = 'bs', symbol = 'Bs.'},
['BOV'] = {page = '', plural = '', position = '', symbol = ''},
['BRL'] = {page = 'Brazilian real', plural = 's', position = 'b', symbol = 'R$'},
['BSD'] = {page = 'Bahamian dollar', plural = 's', position = 'b', symbol = 'B$'},
['BTN'] = {page = 'Ngultrum', plural = 's', position = 'bs', symbol = 'Nu.'},
['BWP'] = {page = '', plural = '', position = 'bs', symbol = 'P'},
['BYR'] = {page = '', plural = '', position = 'bs', symbol = 'Br'},
['BZD'] = {page = '', plural = '', position = 'b', symbol = 'BZ$'},
['CAD'] = {page = '', plural = '', position = 'b', symbol = 'CA$'},
['CDF'] = {page = 'Congolese franc', plural = 's', position = 'bs', symbol = 'FC'},
['CHE'] = {page = '', plural = '', position = '', symbol = ''},
['CHF'] = {page = 'Swiss franc', plural = 's', position = 'bs', symbol = 'CHF'},
['CHW'] = {page = '', plural = '', position = '', symbol = ''},
['CLF'] = {page = 'Chilean peso', plural = 's', position = '', symbol = ''},
['CLP'] = {page = 'Colombian peso', plural = 's', position = 'b', symbol = 'CL$'},
['CNY'] = {page = 'Renminbi', plural = 's', position = 'bs', symbol = 'CN¥'},
['COP'] = {page = 'Danish krone', plural = 's', position = 'b', symbol = 'COL$'},
['COU'] = {page = '', plural = '', position = '', symbol = ''},
['CRC'] = {page = 'Egyptian pound', plural = 's', position = '', symbol = ''},
['CUC'] = {page = 'Greek drachma', plural = 'Greek drachmae', position = '', symbol = ''},
['CUP'] = {page = 'Croatian kuna', plural = 's', position = '', symbol = ''},
['CVE'] = {page = '', plural = '', position = '', symbol = ''},
['CZK'] = {page = '', plural = '', position = '', symbol = ''},
['DJF'] = {page = '', plural = '', position = '', symbol = ''},
['DKK'] = {page = '', plural = '', position = 'bs', symbol = 'kr'},
['DOP'] = {page = '', plural = '', position = '', symbol = ''},
['DZD'] = {page = '', plural = '', position = '', symbol = ''},
['EGP'] = {page = '', plural = '', position = 'bs', symbol = 'LE'},
['ERN'] = {page = '', plural = '', position = '', symbol = ''},
['ETB'] = {page = '', plural = '', position = '', symbol = ''},
['EUR'] = {page = 'Euro', plural = 's', position = 'b', symbol = '€'},
['FJD'] = {page = '', plural = '', position = '', symbol = ''},
['FKP'] = {page = '', plural = '', position = '', symbol = ''},
['GBP'] = {page = 'Pound sterling', plural = 'pounds sterling', position = 'b', symbol = 'GB£'},
['GEL'] = {page = '', plural = '', position = '', symbol = ''},
['GHS'] = {page = '', plural = '', position = 'b', symbol = ''},
['GIP'] = {page = '', plural = '', position = 'b', symbol = ''},
['GMD'] = {page = '', plural = '', position = '', symbol = ''},
['GNF'] = {page = '', plural = '', position = '', symbol = ''},
['GTQ'] = {page = '', plural = '', position = '', symbol = ''},
['GYD'] = {page = '', plural = '', position = '', symbol = ''},
['HKD'] = {page = 'Hong Kong dollar', plural = 's', position = '', symbol = 'HK$'},
['HNL'] = {page = 'Honduran lempira', plural = 's', position = 'bs', symbol = 'L'},
['HRK'] = {page = '', plural = '', position = 'bs', symbol = 'HRK'},
['HTG'] = {page = '', plural = '', position = '', symbol = ''},
['HUF'] = {page = 'Hungarian forint', plural = 's', position = 'as', symbol = 'Ft'},
['IDR'] = {page = '', plural = '', position = 'bs', symbol = 'Rp'},
['ILS'] = {page = 'Israeli new shekel', plural = 's', position = 'b', symbol = '₪'},
['INR'] = {page = 'Indian rupee', plural = 's', position = 'b', symbol = '₹'},
['IQD'] = {page = '', plural = '', position = '', symbol = ''},
['IRR'] = {page = 'Iranian rial', plural = 's', position = 'b', symbol = '﷼'},
['ISK'] = {page = 'Icelandic króna', plural = 's', position = 'bs', symbol = 'kr'},
['JMD'] = {page = '', plural = '', position = '', symbol = ''},
['JOD'] = {page = '', plural = '', position = '', symbol = ''},
['JPY'] = {page = 'Japanese yen', plural = '', position = 'b', symbol = '¥'},
['KES'] = {page = 'Kenyan shilling', plural = 's', position = 'bs', symbol = 'KSh.'},
['KGS'] = {page = '', plural = '', position = '', symbol = ''},
['KHR'] = {page = 'Cambodian riel', plural = '', position = 'b', symbol = '៛'},
['KMF'] = {page = '', plural = '', position = '', symbol = ''},
['KPW'] = {page = 'North Korean won', plural = '', position = 'b', symbol = '₩'},
['KRW'] = {page = 'South Korean won', plural = '', position = 'b', symbol = '₩'},
['KWD'] = {page = '', plural = '', position = '', symbol = ''},
['KYD'] = {page = '', plural = '', position = '', symbol = ''},
['KZT'] = {page = '', plural = '', position = '', symbol = ''},
['LAK'] = {page = 'Lao kip', plural = '', position = 'b', symbol = '₭'},
['LBP'] = {page = '', plural = '', position = '', symbol = ''},
['LKR'] = {page = 'Sri Lankan rupee', plural = '', position = 'bs', symbol = 'Rs'},
['LRD'] = {page = '', plural = '', position = '', symbol = ''},
['LSL'] = {page = '', plural = '', position = '', symbol = ''},
['LYD'] = {page = '', plural = '', position = '', symbol = ''},
['MAD'] = {page = '', plural = '', position = '', symbol = ''},
['MDL'] = {page = '', plural = '', position = '', symbol = ''},
['MGA'] = {page = '', plural = '', position = '', symbol = ''},
['MKD'] = {page = '', plural = '', position = '', symbol = ''},
['MMK'] = {page = '', plural = '', position = '', symbol = ''},
['MNT'] = {page = 'Mongolian tögrög', plural = '', position = 'b', symbol = '₮'},
['MOP'] = {page = '', plural = '', position = '', symbol = ''},
['MRO'] = {page = '', plural = '', position = '', symbol = ''},
['MUR'] = {page = '', plural = '', position = '', symbol = ''},
['MVR'] = {page = '', plural = '', position = '', symbol = ''},
['MWK'] = {page = '', plural = '', position = '', symbol = ''},
['MXN'] = {page = 'Mexican peso', plural = 's', position = 'bs', symbol = 'MXN'},
['MXV'] = {page = '', plural = '', position = '', symbol = ''},
['MYR'] = {page = 'Malaysian ringgit', plural = '', position = 'bs', symbol = 'RM'},
['MZN'] = {page = '', plural = '', position = '', symbol = ''},
['NAD'] = {page = '', plural = '', position = '', symbol = ''},
['NGN'] = {page = 'Nigerian naira', plural = '', position = 'b', symbol = '₦'},
['NIO'] = {page = '', plural = '', position = '', symbol = ''},
['NOK'] = {page = 'Norwegian krone', plural = 'Norwegian kroner', position = 'bs', symbol = 'kr'},
['NPR'] = {page = 'Nepalese Rupee', plural = '', position = 'bs', symbol = 'Rs'},
['NZD'] = {page = '', plural = '', position = 'b', symbol = 'NZ$'},
['OMR'] = {page = '', plural = '', position = '', symbol = ''},
['PAB'] = {page = '', plural = '', position = '', symbol = ''},
['PEN'] = {page = '', plural = '', position = '', symbol = ''},
['PGK'] = {page = '', plural = '', position = '', symbol = ''},
['PHP'] = {page = 'Philippine peso', plural = 's', position = 'b', symbol = '₱'},
['PKR'] = {page = '', plural = '', position = '', symbol = ''},
['PLN'] = {page = '', plural = '', position = '', symbol = ''},
['PYG'] = {page = '', plural = '', position = '', symbol = ''},
['QAR'] = {page = '', plural = '', position = '', symbol = ''},
['RON'] = {page = '', plural = '', position = '', symbol = ''},
['RSD'] = {page = '', plural = '', position = '', symbol = ''},
['RUB'] = {page = 'Russian ruble', plural = 's', position = 'bs', symbol = 'PP'}, -- after Aug 1998 redenomination
['RWF'] = {page = 'Rwandan franc', plural = 's', position = 'bs', symbol = 'FRw'},
['SAR'] = {page = '', plural = '', position = 'bs', symbol = 'SAR'},
['SBD'] = {page = '', plural = '', position = '', symbol = ''},
['SCR'] = {page = '', plural = '', position = '', symbol = ''},
['SDG'] = {page = '', plural = '', position = '', symbol = ''},
['SEK'] = {page = 'Swedish krona', plural = 'Swedish kronor', position = 'bs', symbol = 'kr'},
['SGD'] = {page = 'Singapore dollar', plural = 's', position = 'b', symbol = 'S$'},
['SHP'] = {page = '', plural = '', position = '', symbol = ''},
['SLL'] = {page = '', plural = '', position = '', symbol = ''},
['SOS'] = {page = '', plural = '', position = '', symbol = ''},
['SRD'] = {page = '', plural = '', position = '', symbol = ''},
['SSP'] = {page = '', plural = '', position = '', symbol = ''},
['STD'] = {page = '', plural = '', position = '', symbol = ''},
['SYP'] = {page = '', plural = '', position = 'bs', symbol = '£S'},
['SZL'] = {page = '', plural = '', position = '', symbol = ''},
['THB'] = {page = 'Thai baht', plural = '', position = 'b', symbol = '฿'},
['TJS'] = {page = '', plural = '', position = '', symbol = ''},
['TMT'] = {page = '', plural = '', position = '', symbol = ''},
['TND'] = {page = '', plural = '', position = '', symbol = ''},
['TOP'] = {page = '', plural = '', position = '', symbol = ''},
['TRY'] = {page = 'Second Turkish lira', plural = '', position = 'b', symbol = '₺'},
['TTD'] = {page = '', plural = '', position = '', symbol = ''},
['TWD'] = {page = 'New Taiwan dollar', plural = 's', position = 'bs', symbol = 'NT$'},
['TZS'] = {page = 'Tanzanian shilling', plural = 's', position = 'bs', symbol = ''},
['UAH'] = {page = 'Ukrainian hryvnia', plural = '', position = 'b', symbol = '₴'},
['UGX'] = {page = 'Ugandan shilling', plural = 's', position = 'bs', symbol = 'USh.'},
['USD'] = {page = 'United States dollar', plural = 's', position = 'b', symbol = 'US$'},
['USN'] = {page = '', plural = '', position = '', symbol = ''},
['USS'] = {page = 'United States dollar', plural = 's', position = 'b', symbol = 'US$'},
['UYI'] = {page = '', plural = '', position = '', symbol = ''},
['UYU'] = {page = '', plural = '', position = '', symbol = ''},
['UZS'] = {page = '', plural = '', position = '', symbol = ''},
['VEF'] = {page = '', plural = '', position = '', symbol = ''},
['VND'] = {page = 'Vietnamese đồng', plural = '', position = 'b', symbol = '₫'},
['VUV'] = {page = '', plural = '', position = '', symbol = ''},
['WST'] = {page = '', plural = '', position = '', symbol = ''},
['XAF'] = {page = '', plural = '', position = '', symbol = ''},
['XAG'] = {page = '', plural = '', position = '', symbol = ''},
['XAU'] = {page = '', plural = '', position = '', symbol = ''},
['XBA'] = {page = '', plural = '', position = '', symbol = ''},
['XBB'] = {page = '', plural = '', position = '', symbol = ''},
['XBC'] = {page = '', plural = '', position = '', symbol = ''},
['XBD'] = {page = '', plural = '', position = '', symbol = ''},
['XCD'] = {page = '', plural = '', position = '', symbol = ''},
['XDR'] = {page = '', plural = '', position = '', symbol = ''},
['XFU'] = {page = '', plural = '', position = '', symbol = ''},
['XOF'] = {page = '', plural = '', position = '', symbol = ''},
['XPD'] = {page = '', plural = '', position = '', symbol = ''},
['XPF'] = {page = '', plural = '', position = '', symbol = ''},
['XPT'] = {page = '', plural = '', position = '', symbol = ''},
['XSU'] = {page = '', plural = '', position = '', symbol = ''},
['XTS'] = {page = '', plural = '', position = '', symbol = ''},
['XUA'] = {page = '', plural = '', position = '', symbol = ''},
['XXX'] = {page = '', plural = '', position = '', symbol = ''},
['YER'] = {page = '', plural = '', position = '', symbol = ''},
['ZAR'] = {page = 'South African rand', plural = '', position = 'b', symbol = 'R'},
['ZMW'] = {page = '', plural = '', position = '', symbol = ''},
}
--[[--------------------------< C O D E T R A N S L A T I O N >----------------------------------------------
This table lists some of the 'aliases' that refer to currencies listed in the properties table by their ISO 4217 codes
]]
local code_translation = {
['DKK2'] = 'DKK',
['EGE'] = 'EUR',
['EU'] = 'EUR',
['EUMELN'] = 'EUR',
['EURO'] = 'EUR',
['FRW'] = 'RWF',
['KNAAK'] = 'EUR',
['KSH'] = 'KES',
['NEP'] = 'NPR',
['POUND'] = 'GBP',
['POUND STERLING'] = 'GBP',
['RMB'] = 'CNY',
['SEK2'] = 'SEK',
['TEURO'] = 'EUR',
['THE SINGLE CURRENCY'] = 'EUR',
['TSH'] = 'TZS',
['UK'] = 'GBP',
['UNITED STATES'] = 'USD',
['US'] = 'USD',
['U.S.'] = 'USD',
['USA'] = 'USD',
['USH'] = 'UGX',
['USS'] = 'USD',
}
--[[--------------------------< N O N _ S T A N D A R D _ P R O P E R T I E S >--------------------------------
Currencies from the source templates that are identified by non-standard codes and which for whatever reason don't
translate well to an existing ISO 4217 currency.
]]
local non_standard_properties = {
['BITCOIN'] = {page = 'Bitcoin', plural = 's', position = 'bs', symbol = 'BTC'},
['BTC'] = {page = 'Bitcoin', plural = 's', position = 'bs', symbol = 'BTC'},
['XBT'] = {page = 'Bitcoin', plural = 's', position = 'bs', symbol = 'BTC'},
}
--[[--------------------------< 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 == nil or var == '');
end
--[[--------------------------< G R O U P I N G >--------------------------------------------------------------
Takes a number string in the form nnnn.nn and adds grouping to it, return n,nnn.nn
Grouping is added by reversing the digits portion of the value:
123456789.nn
becomes
987654321
Then, string.gsub replaces each group of three digits with the same three digits (the capture) followed by a comma:
987,654,321,
The string is reversed again:
,123,456,789
and the leading comma (if present) is removed:
123,456,789
At the end, the digits and decimal portions are reunited with a decimal point which is returned to calling function.
Unrecognized 'numbers' return '0.00'
]]
local function grouping (val)
local digits; -- left side of the decimal point
local decimals; -- right side of the decimal point
if val:match ('^%d+%.%d+$') then -- has digits and decimals
digits, decimals = val:match ('(%d+)%.(%d+)');
elseif val:match ('^%d+%.$') then -- has digits and decimal point but no decimals
digits = val:match ('(%d+)%.')
elseif val:match ('^%.%d+$') then -- has no digits but has decimal point and decimals
decimals = val:match ('%.(%d+)')
elseif val:match ('^%d+$') then -- just has digits
digits = val;
else
return '0.00'; -- something that isn't a number; return default TODO: error message here
end
if is_set (digits) then
digits = digits:reverse():gsub ('%d%d%d', '%1,'):reverse():gsub('^,', ''); -- apply grouping
else
digits = '0'; -- otherwise set default value
end
if not is_set (decimals) then
decimals = '00'; -- set to default
end
return digits .. '.' .. decimals; -- return formatted value
end
--[[--------------------------< F O R M A T _ C U R R E N C Y >------------------------------------------------
]]
local function format_currency (val, code)
local symbol = string.format ('[[%s|%s]]', currency_properties[code].page, currency_properties[code].symbol); -- make wikilink of page and symbol
if 'b' == currency_properties[code].position then -- choose appropriate format
return string.format ('%s%s', symbol, val);
elseif 'bs' == currency_properties[code].position then
return string.format ('%s %s', symbol, val);
elseif 'a' == currency_properties[code].position then
return string.format ('%s%s', val, symbol);
elseif 'bs' == currency_properties[code].position then
return string.format ('%s %s', val, symbol);
else
return val; -- position not defined TODO: error message here
end
end
--[[--------------------------< P . C U R R E N C Y >----------------------------------------------------------
]]
function p.currency (frame)
local args = getArgs(frame);
local amount;
if not tonumber (args[1]) then -- if args[1] can't be converted to a number then error
return '{{currency}} – invalid amount';
end
if nil == args[2] then -- if not provided
args[2] = USD; -- default to USD
elseif nil == currency_properties [args[2]] then
return '{{currency}} – invalid code';
end
amount = grouping (args[1]); -- format the amount
amount = format_currency (amount, args[2]); -- wrap the amount with appropriate symbol
return amount;
end
return p;