Module:PercentageCalculator: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 22: | Line 22: | ||
local output = {} | local output = {} | ||
for i, value in ipairs(percentages) do | for i, value in ipairs(percentages) do | ||
table.insert(output, tostring(value)) | if i == 1 then | ||
table.insert(output, tostring(value)) | |||
else | |||
table.insert(output, tostring(math.floor(value + 0.5))) -- Round to the nearest integer | |||
end | |||
end | end | ||
Revision as of 15:40, 1 May 2024
Documentation for this module may be created at Module:PercentageCalculator/doc
-- Function to calculate percentages of a number
local p = {}
function p.calculatePercentages(frame)
local input = tonumber(frame.args[1])
if not input then
return "Invalid input. Please enter a valid number."
end
local originalNumber = input
local percentages = {originalNumber}
-- Calculate percentages
local percentagesToSubtract = {0.15, 0.25, 0.30, 0.35, 0.40}
for i, percent in ipairs(percentagesToSubtract) do
local subtractedValue = originalNumber * percent
originalNumber = originalNumber - subtractedValue
table.insert(percentages, originalNumber)
end
-- Format the output
local output = {}
for i, value in ipairs(percentages) do
if i == 1 then
table.insert(output, tostring(value))
else
table.insert(output, tostring(math.floor(value + 0.5))) -- Round to the nearest integer
end
end
return table.concat(output, " ")
end
return p