Module:Currency: Difference between revisions

Jump to navigation Jump to search
Content added Content deleted
No edit summary
No edit summary
Line 3: Line 3:
function p.main(frame)
function p.main(frame)
local args = frame:getParent().args
local args = frame:getParent().args
local a = args[1] or args.Amount or args.amount or '0'
local a = args[1] or args.Amount or args.amount or '0'
return p.parse(a)
return p.parse(a)
end
end


function p.parse(a)
function p.parse(a)
val = tonumber(a)
local val = tonumber(a)
if val == nil then
if val == nil then
return nil
return nil
end
end
-- Initialize output string
local output = ''
local output = ''
-- Calculate gold, silver, and copper values
local gold = math.floor(val / 1000000)
local gold = math.floor(val / 1000000)
local silver = math.floor((val % 1000000) / 1000)
local copper = val % 1000

-- Append gold if present
if gold > 0 then
if gold > 0 then
output = gold .. ' [[File:Gold coin.png|link=]] '
output = output .. gold .. ' [[File:Gold coin.png|link=]] '
val = val - (gold * 1000000)
end
end
local silver = math.floor(val / 1000)
if (silver > 0) or (gold > 0) then
-- Append silver if present
if silver > 0 then
output = output .. silver .. ' [[File:Silver coin.png|link=]] '
output = output .. silver .. ' [[File:Silver coin.png|link=]] '
val = val - (silver * 1000)
end
end
if (val > 0 and (silver > 0 or gold > 0)) then
-- Append copper if present or if total value is exactly 0
output = output .. val .. ' [[File:Copper coin.png|link=]]'
if copper > 0 or val == 0 then
else
output = val .. ' [[File:Copper coin.png|link=]]'
output = output .. copper .. ' [[File:Copper coin.png|link=]]'
end
end

return output
return output
end
end