話不多說稠通,直接看效果:
1.月供明細(xì)圖:
image.png
2.還貸款明細(xì)圖
image.png
下面直接上代碼
export class LoadHelper {
// 貸款本金
// let totalMoney;
// 貸款月數(shù)
// let month;
// 貸款年利率
// let yearRatio;
// 貸款月利率
// let monthRatio;
// 起息日
//let starData
constructor(totalMoney, month, yearRatio, starData) {
this.totalMoney = totalMoney;
this.month = month;
this.yearRatio = yearRatio;
this.monthRatio = yearRatio / 12;
this.starData = starData;
}
// 某個(gè)月的日利率
dayRation(mDate) {
let days = this.getCountDays(mDate);
return this.monthRatio / days;
}
// 第n期月供明細(xì)
getNPayDetail(n) {
return {
captial: this.getPayNMonthMoney(n),
ration: this.getPayNMonthRation(n)
};
}
// 每月應(yīng)還本金 貸款本金*月利率*(1+月利率)^(還款月序號(hào)-1)/((1+月利率)^還款月數(shù)-1)
getPayNMonthMoney(n) {
return this.totalMoney * this.monthRatio * Math.pow((1 + this.monthRatio), (n - 1)) / (Math.pow((1 + this.monthRatio), this.month) - 1);
}
// 每月應(yīng)還利息 貸款本金*月利率*[(1+月利率)^ 還款月數(shù)-(1+月利率)^(還款月序號(hào)-1)]/[(1+月利率)^還款月數(shù)-1]
getPayNMonthRation(n) {
if (n == 1) {
// 第一期的月有多少天
let days = this.getCountDays(this.starData);
// 第一期用了多少天
let useDays = days - new Date(this.starData).getDate() + 1;
// 第一期的利息多少
return useDays * this.dayRation(this.starData) * this.totalMoney;
}
return this.totalMoney * this.monthRatio * (Math.pow((1 + this.monthRatio), this.month) - Math.pow((1 + this.monthRatio), (n - 1))) / (Math.pow((1 + this.monthRatio), this.month) - 1);
}
// 月供計(jì)算器
// 每月還款額=貸款本金×[月利率×(1+月利率) ^ 還款月數(shù)]÷{[(1+月利率) ^ 還款月數(shù)]-1}
getMonthPay() {
// 月利率
let row = Math.pow(1 + this.monthRatio, this.month);
let result = this.totalMoney * this.monthRatio * row / (row - 1);
return result;
}
// 一共還了多少本金
payTotalCapital(n) {
let result = 0;
for (let i = n; i > 0; i--) {
result += this.getPayNMonthMoney(i);
}
return result;
}
// 一共還了多少利息
payTotalRation(n) {
let result = 0;
for (let i = n; i > 0; i--) {
result += this.getPayNMonthRation(i);
}
return result;
}
// 還剩多少本金未還
needPayCaptial(n) {
return this.totalMoney - this.payTotalCapital(n);
}
// 按日期查詢還剩多少本金未還
needPayCaptialByData(){
let d = new Date(this.starData);
let reduce = new Date().getTime()-d.getTime();
let n = Math.round(reduce/1000/3600/24/30);
return this.needPayCaptial(n);
}
// 獲取當(dāng)月的天數(shù)
getCountDays(mDate) {
let curDate = new Date(mDate);
/* 獲取當(dāng)前月份 */
let curMonth = curDate.getMonth();
/* 生成實(shí)際的月份: 由于curMonth會(huì)比實(shí)際月份小1, 故需加1 */
curDate.setMonth(curMonth + 1);
/* 將日期設(shè)置為0, 這里為什么要這樣設(shè)置, 我不知道原因, 這是從網(wǎng)上學(xué)來的 */
curDate.setDate(0);
/* 返回當(dāng)月的天數(shù) */
return curDate.getDate();
}
}
使用
this.lodaClass = new LoadHelper(410000,360,0.0539,"2018-04-27");