计算表单处理中的百分比



我有一个简单的计算器表格,要求提供基本的预算信息。

用户名,薪水1金额,薪水2金额,租金,水电费.....

不知何故,我需要将所有费用字段单独计算为总收入的百分比。我只是不知道该怎么做。我已经浏览了大量的教程,即使使用源文件,它们都不起作用。

这是我的处理 php。当然可以在正确的方向上推动。谢谢!

    <?php
    if ($_POST['sum1'] == "yes") 
    { 
    $name = $_POST['name']; 
    $month = $_POST['month']; 
    $year = $_POST['year']; 
    $pay1 = $_POST['pay1']; 
    $pay2 = $_POST['pay2']; 
    $other1 = $_POST['other1']; 
    $other2 = $_POST['other2']; 
    $rent = $_POST['rent']; 
    $repair = $_POST['repair']; 
    $utility = $_POST['utility']; 
    $invest = $_POST['invest']; 
    $insure = $_POST['insure']; 
    $groceries = $_POST['groceries']; 
    $student = $_POST['student']; 
    $travel = $_POST['travel']; 
    $ccard1 = $_POST['ccard1']; 
    $ccard2= $_POST['ccard2']; 
    $cell = $_POST['cell']; 
    $cable = $_POST['cable']; 
    $misc1 = $_POST['misc1']; 
    $misc2 = $_POST['misc2'];
    $ttl_inc = $pay1+$pay2+$other1+$other2; 
    $ttl_exp = $rent+$repair+$utility+$invest+$insure+$travel+$ccard1+$ccard2+$cell+$cable+$misc1+$misc2+$student+$groceries; 
    $total = $pay1+$pay2+$other1+$other2-$rent-$repair-$utility-$invest-$insure-$travel-$ccard1-$ccard2-$cell-$cable-$misc1-$misc2-$student-$groceries;  
    echo "<h2>Budget Recap Prepared For {$name} For Period: {$month} {$year}</h2> <br />";
    echo "<br />";
    echo "<h3>Income</h3> <br />";
    echo "<br />";
    echo "Pay Period 1: $ {$pay1} <br />";
    echo "Pay Period 2: $ {$pay2} <br />";
    echo "Other Income 1: $ {$other1} <br />";
    echo "Other Income 2: $ {$other2} <br />";
    echo "<strong>TOTAL INCOME: $ {$ttl_inc} </strong><br />";
    echo "<br />";
    echo "<h3>Expenses</h3> <br />";
    echo "<br />";
    echo "Rent: $ {$rent} <br />";
    echo "Utilities: $ {$utility} <br />";
    echo "Groceries: $ {$groceries} <br />";
    echo "Insurance: $ {$insure} <br />";
    echo "Credit Card 1: $ {$ccard1} <br />";
    echo "Credit Card 2: $ {$ccard2} <br />";
    echo "Repairs/Maintenance: $ {$repair} <br />";
    echo "Savings/Investments: $ {$invest} <br />";
    echo "Student Loans: $ {$student} <br />";
    echo "Transportation: $ {$travel} <br />";
    echo "Cell/Mobile Phone: $ {$cell} <br />";
    echo "Cable/Internet/Other: $ {$cable} <br />";
    echo "Other Expenses 1: $ {$misc1} <br />";
    echo "Other Expenses 2: $ {$misc2} <br />";
    echo "<strong>TOTAL EXPENSES: $ - {$ttl_exp} </strong><br />";
    echo "<br />";
    echo "<strong>NET INCOME FOR {$month} {$year} :  $ {$total} </strong><br />";
    }
    ?>

百分比是表示为100的分数的比率。租金与总收入的比率是租金/总收入例如,如果租金为 30,总收入为 100,则比率 = 0.33333然后你必须把它乘以 100 得到 33.333,使用 round() 函数将摆脱不需要的小数位,你会得到 33 加上 % 符号,你会得到一个格式良好的百分比。

例如:

$rent_percentage = round(($rent / $ttl_inc) * 100) . '%';

最新更新