User:DaxServer/Module Indian Rupee

local p		 = {} local getArgs = require('Module:Arguments').getArgs local yesno  = require('Module:Yesno') local lang	 = mw.language.getContentLanguage; local MINUS  = '−'  -- Unicode U+2212 MINUS SIGN (UTF-8: e2 88 92)

local function format(num) local parsedNumber = lang:parseFormattedNumber(num) return parsedNumber and lang:formatNum(parsedNumber) or num end

-- Returns: number of elements, split table local function validWithMinus(num) local split = mw.text.split(num, '-', true) if #split == 1 then                    -- Input: 100 return {1, split} end if #split == 2 then for k, v in pairs(split) do			if k == 1 and v == '' then return {1, split}      	-- Input: -100 (Minus 100) end end return {2, split}              	-- Input: 100-1000 end return {3, split}						-- unacceptable format end

local function formatnum(num) local data = validWithMinus(num) if data[1] == 1 then                       					-- Input: 100 return format(num) elseif data[1] == 2 then										-- Input: -100 (Minus 100) return format(data[2][1]) .. MINUS .. format(data[2][2])   -- Input: 100-1000 end -- Disable range for Inflation error('Invalid format')		                                   -- Input: -1000--100 or 1-2-3 end

function p.main( frame ) local args  = getArgs(frame) local code  = 'INR' frame = mw.getCurrentFrame local symbol local properties local presentation = mw.loadData ('Module:Currency/Presentation'); 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 ' US$ – invalid code (help) '; end

if yesno(args['link']) or false then symbol = string.format ('%s', properties[code].page, properties[code].symbol);	-- make wikilink of page and symbol else symbol = properties[code].symbol; end local inflationText = '' local year = tonumber(args[2] or args['year'] or 0) local deformattedNum if year ~= 0 then local data = validWithMinus(args[1]) if data[1] == 3 then error('Invalid format') end local startyear = tonumber(frame:expandTemplate{ title = 'Inflation/IN/startyear' }) local lastyear = tonumber(frame:expandTemplate{ title = 'Inflation/year', args = { 'IN' } }) if startyear < year and year < lastyear then inflationText = '&#32;('			local inflation			local inflationFormatted = 			if (args['about'] or ) ~= '' then				inflationText = mw.ustring.format ('%sequivalent to about %s', inflationText, symbol)				if data[1] == 1 then					deformattedNum = lang:parseFormattedNumber(args[1])					inflation = frame:expandTemplate{ title = 'Inflation', args = {'IN', deformattedNum, year, r = args['round'] or 0}}					inflationFormatted = mw.ustring.format ('%s%s', inflationFormatted, format(inflation))				end				if data[1] == 2 then					deformattedNum = lang:parseFormattedNumber(data[2][1])					inflation = frame:expandTemplate{ title = 'Inflation', args = {'IN', deformattedNum, year, r = args['round'] or 0}}					inflationFormatted = mw.ustring.format ('%s%s', inflationFormatted, format(inflation))					deformattedNum = lang:parseFormattedNumber(data[2][2])					inflation = frame:expandTemplate{ title = 'Inflation', args = {'IN', deformattedNum, year, r = args['round'] or 0}} inflationFormatted = mw.ustring.format ('%s%s%s', inflationFormatted, MINUS, format(inflation)) end else inflationText = mw.ustring.format ('%sequivalent to %s', inflationText, symbol) local round if data[1] == 1 then deformattedNum = lang:parseFormattedNumber(args[1]) inflation = tonumber(frame:expandTemplate{ title = 'Inflation', args = {'IN', deformattedNum, year}}) round = args['round'] or ((inflation > 1000 or inflation < -1000) and 0 or 2) inflation = frame:expandTemplate{ title = 'Inflation', args = {'IN', deformattedNum, year, r = round}} inflationFormatted = mw.ustring.format ('%s%s', inflationFormatted, format(inflation)) end if data[1] == 2 then deformattedNum = lang:parseFormattedNumber(data[2][1]) inflation = tonumber(frame:expandTemplate{ title = 'Inflation', args = {'IN', deformattedNum, year}}) round = args['round'] or ((inflation > 1000 or inflation < -1000) and 0 or 2) inflation = frame:expandTemplate{ title = 'Inflation', args = {'IN', deformattedNum, year, r = round}} inflationFormatted = mw.ustring.format ('%s%s', inflationFormatted, format(inflation)) deformattedNum = lang:parseFormattedNumber(data[2][2]) inflation = tonumber(frame:expandTemplate{ title = 'Inflation', args = {'IN', deformattedNum, year}}) round = args['round'] or ((inflation > 1000 or inflation < -1000) and 0 or 2) inflation = frame:expandTemplate{ title = 'Inflation', args = {'IN', deformattedNum, year, r = round}} inflationFormatted = mw.ustring.format ('%s%s%s', inflationFormatted, MINUS, format(inflation)) end end inflationText = mw.ustring.format ('%s%s in %s)', inflationText, inflationFormatted, lastyear)		end	end	local formattedvalue = args[1] == nil and '' or formatnum(args[1])	return mw.ustring.format(' %s%s%s ', symbol, formattedvalue, inflationText) end

return p