Module:Currency: Difference between revisions
m
handle negative numbers
No edit summary |
Microbrews (talk | contribs) m (handle negative numbers) |
||
(11 intermediate revisions by 3 users not shown) | |||
Line 3:
function p.main(frame)
local args = frame:getParent().args
end
function p.parse(a)
local val = tonumber(a)
if val == nil then
return nil
end
-- Initialize output string
local output = ''
local gold = math.floor(val / 1000000)▼
local isNegative = val < 0
local prefix = ""
if isNegative then
val = -val
prefix = "-"
end
-- Calculate gold, silver, and copper values
local copper = val % 1000
-- Append gold if present
if gold > 0 then
output = prefix .. output .. gold .. ' [[File:Gold coin.png|link=]] '
end
-- Append silver if present
output = output .. prefix .. silver .. ' [[File:Silver coin.png|link=]] '▼
end
▲ local silver = math.floor(val / 1000)
-- Append copper if present or if total value is exactly 0
▲ if (silver > 0) or (gold > 0) then
if copper > 0 or val == 0 then
▲ output = output .. silver .. '[[File:Silver coin.png|link=]] '
end
▲ output = output .. val .. '[[File:Copper coin.png|link=]]'
return output
end
|