Module:PercentageCalculator: Difference between revisions

From Roat Pkz
Jump to navigation Jump to search
(Created page with "-- 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 percentages = {input} -- Calculate percentages local percentagesToAdd = {0.15, 0.25, 0.30, 0.35, 0.40} for i, percent in ipairs(percentagesToAdd) do table.insert(percentages, input * percent) end --...")
 
No edit summary
Line 10: Line 10:
     local percentages = {input}
     local percentages = {input}


     -- Calculate percentages
     -- Calculate percentages and subtract from the original number
     local percentagesToAdd = {0.15, 0.25, 0.30, 0.35, 0.40}
     local percentagesToSubtract = {0.15, 0.25, 0.30, 0.35, 0.40}
     for i, percent in ipairs(percentagesToAdd) do
     for i, percent in ipairs(percentagesToSubtract) do
         table.insert(percentages, input * percent)
        local subtractedValue = input * percent
         table.insert(percentages, subtractedValue)
        input = input - subtractedValue
     end
     end


Line 20: Line 22:
     table.insert(output, "Original number: " .. tostring(percentages[1]))
     table.insert(output, "Original number: " .. tostring(percentages[1]))
     for i = 2, #percentages do
     for i = 2, #percentages do
         table.insert(output, tostring((percentagesToAdd[i - 1] * 100)) .. "%: " .. tostring(percentages[i]))
         table.insert(output, tostring((percentagesToSubtract[i - 1] * 100)) .. "% subtracted: " .. tostring(percentages[i]))
     end
     end



Revision as of 15:36, 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 percentages = {input}

    -- Calculate percentages and subtract from the original number
    local percentagesToSubtract = {0.15, 0.25, 0.30, 0.35, 0.40}
    for i, percent in ipairs(percentagesToSubtract) do
        local subtractedValue = input * percent
        table.insert(percentages, subtractedValue)
        input = input - subtractedValue
    end

    -- Format the output
    local output = {}
    table.insert(output, "Original number: " .. tostring(percentages[1]))
    for i = 2, #percentages do
        table.insert(output, tostring((percentagesToSubtract[i - 1] * 100)) .. "% subtracted: " .. tostring(percentages[i]))
    end

    return table.concat(output, "\n")
end

return p