一、等额本金
// (等额本金)
//$loanAmount=>贷款金额
//$loanPeriod=>贷款年限
//$interestRate=>贷款利息
function calculateEqualPrincipalPayment($loanAmount, $loanPeriod, $interestRate)
{
$monthlyPrincipal = $loanAmount / ($loanPeriod * 12);
$monthlyInterest = $loanAmount * ($interestRate / 100) / 12;
$remainingAmount = $loanAmount;
$result = [];
for ($i = 1; $i <= $loanPeriod * 12; $i++) {
$interest = $remainingAmount * ($interestRate / 100) / 12;
$principal = $monthlyPrincipal;
$remainingAmount -= $monthlyPrincipal;
$monthlyPayment = $principal + $interest;
$result[$i] = [
'month' => $i,//期数
'principal' => round($principal, 2),//月供本金
'interest' => round($interest, 2),//月供利息
'monthlyPayment' => round($monthlyPayment, 2),//月供总额
'remainingAmount' => round($remainingAmount, 2),//剩余本金
];
}
return $result;
}
二、等额本息
// (等额本息)
//$loanAmount=>贷款金额
//$loanPeriod=>贷款年限
//$loanPeriod=>贷款利率
function calculateEqualInstallmentPayment($loanAmount, $loanPeriod, $interestRate)
{
$monthlyInterestRate = $interestRate / 12 / 100;
$numOfMonthlyPayments = $loanPeriod * 12;
$monthlyPayment = $loanAmount * $monthlyInterestRate * pow(1 + $monthlyInterestRate, $numOfMonthlyPayments)
/ (pow(1 + $monthlyInterestRate, $numOfMonthlyPayments) - 1);
$result = [];
for ($i = 1; $i <= $numOfMonthlyPayments; $i++) {
$interest = $loanAmount * $monthlyInterestRate;
$principal = $monthlyPayment - $interest;
$remainingAmount = $loanAmount - $principal;
$result[$i] = [
'month' => $i,//期数
'principal' => round($principal, 2),//期数
'interest' => round($interest, 2),//月供本金
'monthlyPayment' => round($monthlyPayment, 2),//月供总额
'remainingAmount' => round($remainingAmount, 2),//剩余本金
];
$loanAmount = $remainingAmount;
}
return $result;
}
三、公积金和商业贷组合贷款
// 测试组合贷款计算()等额本息
function calculateCombinationLoan($commercialLoanAmount, $commercialLoanPeriod, $commercialInterestRate, $fundLoanAmount, $fundLoanPeriod, $fundInterestRate)
{
// 计算商业贷款的还款计划
$commercialRepayments = calculateEqualInstallmentPayment($commercialLoanAmount, $commercialLoanPeriod, $commercialInterestRate);
// 计算公积金贷款的还款计划
$fundRepayments = calculateEqualInstallmentPayment($fundLoanAmount, $fundLoanPeriod, $fundInterestRate);
$arr_ti = $commercialRepayments;
$arr_ti_duan = $fundRepayments;
$newArray = [];
foreach ($arr_ti as $is => $repayment) {
$shangye_monthlyPayment = $repayment["monthlyPayment"] ?: 0;
$jijin_monthlyPayment = $arr_ti_duan[$is]["monthlyPayment"] ?: 0;
$newArray[] = [
"month" => $repayment["month"],
"jijin_monthlyPayment" => round($jijin_monthlyPayment, 2),
"shangye_monthlyPayment" => round($shangye_monthlyPayment, 2),
"monthlyPayment" => round(($repayment["monthlyPayment"] + $arr_ti_duan[$is]["monthlyPayment"]), 2),
"interest" => round(($repayment["interest"] + $arr_ti_duan[$is]["interest"]), 2),
];
}
return $newArray;
}
// 测试组合贷款计算(等额本金)
function calculatePrincipaltionLoan($commercialLoanAmount, $commercialLoanPeriod, $commercialInterestRate, $fundLoanAmount, $fundLoanPeriod, $fundInterestRate)
{
// 计算商业贷款的还款计划
$commercialRepayments = calculateEqualPrincipalPayment($commercialLoanAmount, $commercialLoanPeriod, $commercialInterestRate);
// 计算公积金贷款的还款计划
$fundRepayments = calculateEqualPrincipalPayment($fundLoanAmount, $fundLoanPeriod, $fundInterestRate);
$arr_ti = $commercialRepayments;
$arr_ti_duan = $fundRepayments;
$newArray = [];
foreach ($arr_ti as $is => $repayment) {
$shangye_monthlyPayment = $repayment["monthlyPayment"] ?: 0;
$jijin_monthlyPayment = $arr_ti_duan[$is]["monthlyPayment"] ?: 0;
$newArray[] = [
"month" => $repayment["month"],
"jijin_monthlyPayment" => round($jijin_monthlyPayment, 2),
"shangye_monthlyPayment" => round($shangye_monthlyPayment, 2),
"monthlyPayment" => round(($repayment["monthlyPayment"] + $arr_ti_duan[$is]["monthlyPayment"]), 2),
"interest" => round(($repayment["interest"] + $arr_ti_duan[$is]["interest"]), 2),
];
}
return $newArray;
}
四、可实现效果
文章来源地址https://www.toymoban.com/news/detail-643385.html
文章来源:https://www.toymoban.com/news/detail-643385.html
到了这里,关于PHP 之房贷计算器、组合贷的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!