Module:New Taiwan dollar

From Wikivoyage
Jump to navigation Jump to search

Documentation for this module may be created at Module:New Taiwan dollar/doc

local p = {}

local function printvalue(a, b) -- Used to create a range separated by a dash
	if b == nil then
		return a
	else
		return a .. '–' .. b
	end
end

local function round(x) -- Returns the input value rounded. Rounds to 3 decimal points if output is smaller than 0.1.
	if math.floor(x * 10^2 + 0.5) / 10^2 >= 0.1 then
		return math.floor(x * 10^2 + 0.5) / 10^2
	else
		return math.floor(x * 10^3 + 0.5) / 10^3
	end
end

local function USD(a) -- Used to convert NT$ to USD
	local r = 29.44 -- Exchange rate (NT$ per $1 USD)
	if a == nil then
		return nil
	else
		return round(a/r, 2)
	end
end

function p.NT(frame) -- Output
	local pf = frame:getParent().args
	local f = frame.args
	local m = f[1] or pf[1] -- First parameter shortened as 'm'
	local M = f[2] or pf[2] -- Second as 'M'
	local l = f.l or pf.l -- Link
	local c = f.c or pf.c -- Convert
	if m ~= nil and l ~= 'y' then
		return 'NT$' .. printvalue(m, M) .. ' (US$' .. printvalue(USD(m), USD(M))  .. ')'
	elseif m ~= nil then
		return '[[Taiwan#Money|NT$]]' .. printvalue(m, M) .. ' (US$' .. printvalue(USD(m), USD(M))  .. ')'
	else
		return '[[Taiwan#Money|New Taiwan dollar]]'
	end
end

return p