Module:Average change: Difference between revisions
Appearance
Content deleted Content added
updated code |
another try |
||
Line 2: | Line 2: | ||
-- Main function to calculate average change |
-- Main function to calculate average change |
||
function p.main( |
function p.main(frame) |
||
local args = frame.args |
|||
-- Collect all variable arguments into a table |
|||
local |
local class = args.class or 'actual' |
||
local omitOp = args.omitOp or 'no' |
|||
local params = { args[1], args[2], args[3], args[4], args[5] } |
|||
-- Filter out nil values and convert to numbers |
-- Filter out nil values and convert to numbers |
||
local values = {} |
local values = {} |
||
Line 14: | Line 16: | ||
end |
end |
||
end |
end |
||
-- Error if fewer than 2 valid values |
-- Error if fewer than 2 valid values |
||
if #values < 2 then |
if #values < 2 then |
||
return 'Error: Insufficient |
return 'Error: Insufficient Parameters' |
||
end |
end |
||
-- Calculate changes |
-- Calculate changes |
||
local changes = {} |
local changes = {} |
||
for i = 1, #values - 1 do |
for i = 1, #values - 1 do |
||
if |
if class == "percent" then |
||
table.insert(changes, ((values[i + 1] - values[i]) / values[i]) * 100) |
table.insert(changes, ((values[i + 1] - values[i]) / values[i]) * 100) |
||
else |
else |
||
Line 29: | Line 31: | ||
end |
end |
||
end |
end |
||
-- Calculate average change |
-- Calculate average change |
||
local sum = 0 |
local sum = 0 |
||
Line 35: | Line 37: | ||
sum = sum + change |
sum = sum + change |
||
end |
end |
||
local avgChange = sum / #changes |
local avgChange = sum / #changes |
||
local formattedChange = string.format("%.2f", avgChange) |
local formattedChange = string.format("%.2f", avgChange) |
||
-- Omit the operator if specified |
-- Omit the operator if specified |
||
if |
if omitOp == "yes" then |
||
formattedChange = string.gsub(formattedChange, "^%-", "") |
formattedChange = string.gsub(formattedChange, "^%-", "") |
||
end |
end |
||
return formattedChange |
return formattedChange |
||
end |
end |
Revision as of 11:17, 27 July 2024
local p = {}
-- Main function to calculate average change
function p.main(frame)
local args = frame.args
local class = args.class or 'actual'
local omitOp = args.omitOp or 'no'
local params = { args[1], args[2], args[3], args[4], args[5] }
-- Filter out nil values and convert to numbers
local values = {}
for _, v in ipairs(params) do
v = tonumber(v)
if v then
table.insert(values, v)
end
end
-- Error if fewer than 2 valid values
if #values < 2 then
return 'Error: Insufficient Parameters'
end
-- Calculate changes
local changes = {}
for i = 1, #values - 1 do
if class == "percent" then
table.insert(changes, ((values[i + 1] - values[i]) / values[i]) * 100)
else
table.insert(changes, values[i + 1] - values[i])
end
end
-- Calculate average change
local sum = 0
for _, change in ipairs(changes) do
sum = sum + change
end
local avgChange = sum / #changes
local formattedChange = string.format("%.2f", avgChange)
-- Omit the operator if specified
if omitOp == "yes" then
formattedChange = string.gsub(formattedChange, "^%-", "")
end
return formattedChange
end
return p