Module:PercentageCalculator: Difference between revisions

From Roat Pkz
Jump to navigation Jump to search
No edit summary
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 11: Line 11:
     local percentages = {originalNumber}
     local percentages = {originalNumber}


     -- Calculate percentages and subtract from the original number
     -- Calculate percentages
     local percentagesToSubtract = {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(percentagesToSubtract) do
     for i, percent in ipairs(percentagesToSubtract) do
         local subtractedValue = input * percent
         local subtractedValue = originalNumber * percent
         table.insert(percentages, subtractedValue)
        originalNumber = originalNumber - subtractedValue
        input = input - subtractedValue
         table.insert(percentages, math.floor(originalNumber + 0.5)) -- Round to the nearest integer
     end
     end


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


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


return p
return p

Latest 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, math.floor(originalNumber + 0.5)) -- Round to the nearest integer
    end

    -- Format the output
    local output = {}
    for i, value in ipairs(percentages) do
        table.insert(output, tostring(value))
    end

    return table.concat(output, " ")
end

return p