我有一个简单的计算器表格,要求提供基本的预算信息。
用户名,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 />";
}
?>
答案 0 :(得分:0)
百分比是表示为100的分数的比率。 租金与总收入的比率是租金/总收入 例如,如果租金为30且总收入为100,则比率= 0.33333 然后你必须将它乘以100得到33.333并使用round()函数将摆脱不需要的小数位,你将获得33添加%符号,你将获得格式良好的百分比。
例如:
$rent_percentage = round(($rent / $ttl_inc) * 100) . '%';