PHP 之房贷计算器、组合贷

 一、等额本金

// (等额本金)
//$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;if ($fundLoanPeriod > $commercialLoanPeriod) {$arr_ti = $fundRepayments;$arr_ti_duan = $commercialRepayments;}$newArray = [];foreach ($arr_ti as $is => $repayment) {if ($fundLoanPeriod < $commercialLoanPeriod) {$shangye_monthlyPayment = $repayment["monthlyPayment"] ?: 0;$jijin_monthlyPayment = $arr_ti_duan[$is]["monthlyPayment"] ?: 0;} else {$shangye_monthlyPayment = $arr_ti_duan[$is]["monthlyPayment"]?: 0;$jijin_monthlyPayment = $repayment["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;if ($fundLoanPeriod > $commercialLoanPeriod) {$arr_ti = $fundRepayments;$arr_ti_duan = $commercialRepayments;}$newArray = [];foreach ($arr_ti as $is => $repayment) {if ($fundLoanPeriod < $commercialLoanPeriod) {$shangye_monthlyPayment = $repayment["monthlyPayment"] ?: 0;$jijin_monthlyPayment = $arr_ti_duan[$is]["monthlyPayment"] ?: 0;} else {$shangye_monthlyPayment = $arr_ti_duan[$is]["monthlyPayment"]?: 0;$jijin_monthlyPayment = $repayment["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;
}

四、可实现效果

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/63875.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

分享一个计算器

先看效果&#xff1a; 再看代码&#xff08;查看更多&#xff09;&#xff1a; <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>计算器</title><style>* {box-sizing: border-box;}body…

JVM 基础

巩固基础&#xff0c;砥砺前行 。 只有不断重复&#xff0c;才能做到超越自己。 能坚持把简单的事情做到极致&#xff0c;也是不容易的。 JVM 类加载机制 JVM 类加载机制分为五个部分&#xff1a;加载&#xff0c;验证&#xff0c;准备&#xff0c;解析&#xff0c;初始化&am…

积木报表集成前端加载js文件404

项目场景&#xff1a; 在集成积木报表和shiro时候&#xff1a; 集成积木报表&#xff0c;shrio&#xff0c;shrio是定义在另一个模块下的&#xff0c;供另一个启动类使用&#xff0c;积木报表集成shrio的时候&#xff0c;需要依赖存放shrio的核心包&#xff0c;该核心包除了存…

7.利用matlab完成 符号方阵的特征值分解和 符号矩阵的奇异值分解 (matlab程序)

1.简述 &#xff08;1&#xff09;特征值分解&#xff1a;函数eig 格式&#xff1a;[V,D] eig(A) %计算A的特征值对角阵D和特征向量V&#xff0c;使AVVD成立。 注意&#xff1a;特征值分解时&#xff0c;使用eig&#xff0c;矩阵A必须是方阵。 A [0 1;1 1]; [V,D] ei…

ADM2587E在RS485和RS422接口的应用(ADM2587E电路原理图和程序开发)

最近做一个项目使用到ADM2587E&#xff0c;为了解决公司历史遗留的问题&#xff08;ADM2587E芯片发烫&#xff0c;容易烧毁&#xff0c;485设备只能手拉手连接三四个&#xff0c;就通信不正常现象&#xff09;&#xff0c;认真阅读了Datasheet和官网LayOut的一些设计文档&#…

Springboot04--vue前端部分+element-ui

注意点&#xff1a; 这边v-model和value的区别&#xff1a;v-model是双向绑定的&#xff0c;value是单向绑定 li的key的问题 vue的组件化开发&#xff1a; 1. NPM&#xff08;类似maven&#xff0c;是管理前段代码的工具&#xff09; 安装完之后可以在cmd里面使用以下指令 2.…

数据库中的连表更新和连表删除

1.连表更新 准备两张表,id一样,但是姓名不一样, 需求根据id让姓名保持一致 执行的sql UPDATE teacher_copy1 AS b INNER JOIN teacher c ON b.TId c.TId set b.tnamec.tname 执行结果 2.连接删除 DELETE a FROMteacher_copy1 AS aINNER JOIN teacher b ON a.TId b.TId

flutter 初识(开发体验,优缺点)

前言 最近有个跨平台桌面应用的需求&#xff0c;需要支持 windows/linux/mac 系统&#xff0c;要做个更新应用的小界面&#xff0c;主要功能就是下载更新文件并在本地进行替换&#xff0c;很简单的小功能。 花了几分钟构建没做 UI 优化的示例界面&#xff1a; 由于我们的客…

STM32F429IGT6使用CubeMX配置串口通信

1、硬件电路 2、设置RCC&#xff0c;选择高速外部时钟HSE,时钟设置为180MHz 3、配置USART1引脚 4、生成工程配置 5、部分代码 //重定向printf函数 int fputc(int ch, FILE *f) {HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xffff);return ch; } /* USER CODE BE…

不基于比较的排序:基数排序

本篇只是讨论桶排序的具体实现&#xff0c;想了解更多算法内容可以在我的博客里搜&#xff0c;建议大家看看这篇排序算法总结&#xff1a;排序算法总结_鱼跃鹰飞的博客-CSDN博客 桶排序的原理&#xff1a; 代码&#xff1a;sort1是一个比较二逼的实现方式浪费空间&#xff0c;s…

[git] git基础知识

git是一个免费的、开源的分布式版本控制系统&#xff0c;可以快速高效地处理从小型到大型的各种项目 git易于学习&#xff0c;性能极快 什么是版本控制&#xff1f; 版本控制是一种记录文件内容变化&#xff0c;以便将来查阅特定版本修订情况&#xff0c;可以记录文件修改历史…

【ElasticSearch入门】

目录 1.ElasticSearch的简介 2.用数据库实现搜素的功能 3.ES的核心概念 3.1 NRT(Near Realtime)近实时 3.2 cluster集群&#xff0c;ES是一个分布式的系统 3.3 Node节点&#xff0c;就是集群中的一台服务器 3.4 index 索引&#xff08;索引库&#xff09; 3.5 type类型 3.6 doc…