Module:Currency: Difference between revisions
Jump to navigation
Jump to search
Content added Content deleted
No edit summary |
Microbrews (talk | contribs) m (handle negative numbers) |
||
(15 intermediate revisions by 3 users not shown) | |||
Line 2: | Line 2: | ||
function p.main(frame) |
function p.main(frame) |
||
local args = |
local args = frame:getParent().args |
||
local a = args[1] or args.Amount or args.amount or '0' |
|||
return p.parse(a) |
|||
end |
|||
function p.parse(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 = '' |
||
⚫ | |||
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 |
if gold > 0 then |
||
output = gold .. '[[File:Gold coin.png|link=]] ' |
output = prefix .. output .. gold .. ' [[File:Gold coin.png|link=]] ' |
||
end |
|||
val = val - (gold * 1000000) |
|||
-- Append silver if present |
|||
⚫ | |||
⚫ | |||
end |
end |
||
⚫ | |||
-- Append copper if present or if total value is exactly 0 |
|||
⚫ | |||
if copper > 0 or val == 0 then |
|||
⚫ | |||
⚫ | |||
val = val - (silver * 1000) |
|||
end |
end |
||
⚫ | |||
return output |
return output |
||
end |
end |
Latest revision as of 23:22, 6 November 2024
Documentation for this module may be created at Module:Currency/doc
local p = {}
function p.main(frame)
local args = frame:getParent().args
local a = args[1] or args.Amount or args.amount or '0'
return p.parse(a)
end
function p.parse(a)
local val = tonumber(a)
if val == nil then
return nil
end
-- Initialize output string
local output = ''
local isNegative = val < 0
local prefix = ""
if isNegative then
val = -val
prefix = "-"
end
-- Calculate gold, silver, and copper values
local gold = math.modf(val / 1000000)
local silver = math.modf((val % 1000000) / 1000)
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
if silver > 0 then
output = output .. prefix .. silver .. ' [[File:Silver coin.png|link=]] '
end
-- Append copper if present or if total value is exactly 0
if copper > 0 or val == 0 then
output = output .. prefix .. copper .. ' [[File:Copper coin.png|link=]]'
end
return output
end
return p