Module:Currency: Difference between revisions

296 bytes added ,  Yesterday at 23:22
m
handle negative numbers
No edit summary
m (handle negative numbers)
 
(17 intermediate revisions by 3 users not shown)
Line 1:
local p = {}
 
function p.currencymain(frame)
local args = getArgs(frame:getParent().args
local a = args[1] or args.Amount or args.amount or '0'
valreturn = tonumberp.parse(a)
end
 
function p._mainparse(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 gold = math.floormodf(val / 1000000)
local silver = math.floormodf((val % 1000000) / 1000)
local copper = val % 1000
 
-- Append gold if present
if gold > 0 then
output = prefix .. output .. gold .. ' [[File:Gold coin.png|link=]] '
val = val - (gold * 1000000)
end
local silver = math.floor(val / 1000)
if-- Append (silver > 0) or (gold > 0)if thenpresent
if silver > 0 then
output = output .. prefix .. silver .. ' [[File:Silver coin.png|link=]] '
val = val - (silver * 1000)
end
-- Append copper if present or if total value is exactly 0
if copper > 0 or val == 0 then
output = output .. valprefix .. copper .. ' [[File:Copper coin.png|link=]]'
end
output = output .. val .. '[[File:Copper coin.png|link=]]'
return output
end
 
return output
--
-- Module access point
--
function p._main(a)
a = tostring(a) or '0'
return amount(a)
end
 
3

edits