Module:PriceCalculator: Difference between revisions
No edit summary |
No edit summary |
||
(8 intermediate revisions by the same user not shown) | |||
Line 25: | Line 25: | ||
-- Calculate and return rounded result | -- Calculate and return rounded result | ||
local result = math.floor(price * factor + 0.0) | |||
return math.max(result, 1) | |||
end | end | ||
Line 39: | Line 40: | ||
local args = frame.args | local args = frame.args | ||
local inputPrice = args.price or args[1] or "1" | local inputPrice = args.price or args[1] or "1" | ||
local priceType = args.type or args[2] or "sell" | local priceType = mw.text.trim(args.type or args[2] or "sell") | ||
local min = tonumber(args.min or args.MinimumQuantity or "1") or 1 | local min = tonumber(args.min or args.MinimumQuantity or "1") or 1 | ||
local max = tonumber(args.max or args.MaximumQuantity or "") -- optional | local max = tonumber(args.max or args.MaximumQuantity or "") -- optional | ||
local rawPrice = p._calculate(inputPrice, priceType) | |||
local | if type(rawPrice) ~= "number" then | ||
if | return "Error: " .. tostring(rawPrice) | ||
return "Error: | |||
end | end | ||
local price = rawPrice | |||
local function format(num) | local function format(num) | ||
return mw.getContentLanguage():formatNum(num) | return mw.getContentLanguage():formatNum(num) | ||
end | end | ||
local coinIcon = frame:preprocess('{{CoinIcon|size=16px}}') | |||
local coinIcon = ' | |||
if max and max ~= min then | if max and max ~= min then | ||
if price > 1 then | if price > 1 then | ||
return string.format( | return string.format('<abbr title="%d coins each">%s–%s</abbr> %s', price, format(price * min), format(price * max), coinIcon) | ||
else | else | ||
return string.format('%s–%s%s', format(price * min), format(price * max), coinIcon) | return string.format('%s–%s %s', format(price * min), format(price * max), coinIcon) | ||
end | end | ||
elseif min == 1 then | elseif min == 1 then | ||
return string.format('%s%s', format(price), coinIcon) | return string.format('%s %s', format(price), coinIcon) | ||
else | else | ||
if price > 1 then | if price > 1 then | ||
return string.format( | return string.format('<abbr title="%d coins each">%s</abbr> %s', price, format(price * min), coinIcon) | ||
else | else | ||
return string.format('%s%s', format(price * min), coinIcon) | return string.format('%s %s', format(price * min), coinIcon) | ||
end | end | ||
end | end | ||
end | end | ||
return p | return p |