-1

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

用户名、Paycheck 1 金额、Paycheck 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 />";
    }
    ?>
4

1 回答 1

0

百分比是表示为 100 的分数的比率。租金与总收入的比率是租金/总收入,例如,如果租金是 30,总收入是 100,那么比率 = 0.33333,那么你必须将它乘以 100 到得到 33.333 并使用 round() 函数将去掉不需要的小数位,你会得到 33 添加 % 符号,你会得到一个格式正确的百分比。

例如:

$rent_percentage = round(($rent / $ttl_inc) * 100) . '%';
于 2013-11-10T00:11:26.593 回答