Модуль:ConvertCurrency
Материал из проекта «Повелитель Тайн Вики»
Дополнительные действия
The module to use with {{Currency}} template.
local p = {}
-- [ru] Russian plural: plural(n, 'верль', 'верля', 'верлей')
local function plural(n, one, few, many)
n = math.floor(math.abs(n or 0))
local n10, n100 = n % 10, n % 100
if n10 == 1 and n100 ~= 11 then return one end
if n10 >= 2 and n10 <= 4 and (n100 < 12 or n100 > 14) then return few end
return many
end
local NAMES = {
verl = {'верль', 'верля', 'верлей'},
pound = {'фунт', 'фунта', 'фунтов'},
risot = {'ризот', 'ризота', 'ризотов'},
}
local function cname(cur, n, isMillions)
local f = NAMES[cur]
if not f then return cur end
if isMillions then return f[3] end
return plural(n, f[1], f[2], f[3])
end
-- thousands separator: narrow no-break space (Russian typography)
local function grp(x)
local s = string.format("%0.0f", x)
local out = s
while true do
local k
out, k = out:gsub("^(-?%d+)(%d%d%d)", "%1\226\128\175%2")
if k == 0 then break end
end
return out
end
function p.currency(frame)
local args = frame.args
local amount_str = args[1] or ''
local fromCurrency = args[2] or ''
local toCurrency = args[3] or ''
local CurrencyName = args[4] or ''
-- Remove commas from amount_str
amount_str = amount_str:gsub(",", "")
-- Conversion rates
local poundToVerl = 24
local risotToPound = 1 / 12
local amount = tonumber(amount_str)
if amount == nil then
-- Handle cases where the amount includes abbreviations like "k" or "m"
local suffix = amount_str:sub(-1):lower()
amount = tonumber(amount_str:sub(1, -2))
if suffix == "k" then
amount = amount * 1000
elseif suffix == "m" then
amount = amount * 1000000
end
end
if amount == nil then
return "Некорректная сумма"
end
local result
-- Perform currency conversion based on input currencies
if fromCurrency == "verl" and toCurrency == "pound" then
result = amount / poundToVerl
result = math.ceil(result) -- Round up to nearest whole number
elseif fromCurrency == "pound" and toCurrency == "verl" then
result = amount * poundToVerl
elseif fromCurrency == "risot" and toCurrency == "pound" then
result = amount * risotToPound
result = math.ceil(result) -- Round up to nearest whole number
elseif fromCurrency == "pound" and toCurrency == "risot" then
result = amount / risotToPound
else
return "Неподдерживаемая конвертация"
end
-- Format the result with comma as thousands separator or abbreviation
local formattedAmount
local isMillions = false
if CurrencyName == "mos" then
if amount >= 1000000 then
isMillions = true
formattedAmount = string.format("%.1f млн", amount / 1000000):gsub("%.", ",")
-- Check if the result is a whole number, then remove decimal point and ",0"
if math.floor(amount / 1000000) == (amount / 1000000) then
formattedAmount = string.format("%d млн", amount / 1000000)
end
else
formattedAmount = grp(amount)
end
else
formattedAmount = grp(amount)
end
-- Format the result with comma as thousands separator
local formattedResult = grp(result)
-- Remove trailing ".00" from the formatted result
formattedResult = formattedResult:gsub("%.00$", "")
-- Adjust currency names and add wikilinks
local fromCurrencyName = cname(fromCurrency, amount, isMillions)
if fromCurrency == "verl" then fromCurrencyName = "[[Валюта#Верль|" .. fromCurrencyName .. "]]"
elseif fromCurrency == "risot" then fromCurrencyName = "[[Валюта#Королевство Фейнапоттер|" .. fromCurrencyName .. "]]" end
local toCurrencyName = cname(toCurrency, result, false)
-- Construct output string with tooltip
local Output
if CurrencyName ~= "no" then
Output = string.format('<span class="basic-tooltip" title="≈ %s %s" style="text-decoration:underline dotted; cursor:help;">%s %s</span>', formattedResult, toCurrencyName, formattedAmount, fromCurrencyName)
else
Output = string.format('<span class="basic-tooltip" title="≈ %s %s" style="text-decoration:underline dotted; cursor:help;">%s</span>', formattedResult, toCurrencyName, formattedAmount)
end
return Output
end
return p