Mortgage.js

// @ref: https://www.anjuke.com/calculator/loan?from=PC_Prop_FYXXFangDaiJiSuan_click&prop_id=3027970414124047

//等额本息(等额均还) 
Mortgage = function(){};

Mortgage.prototype = {
  //  payType : 1为等额本息, 2为等额本金
  //[贷款本金×月利率×(1+月利率)^还款月数]÷[(1+月利率)^还款月数-1]
  monthRepay: function(capital,month,monthrate,payType,index){
    var _payType = payType || 1 , _index = index || 0;
    if( _payType == 1){
      var pow = Math.pow( 1 + monthrate, month);
      return capital * monthrate * pow / (pow - 1);
    }else{
      return (capital - _index * capital/ month) * monthrate + capital/ month;
    }
  },
  monthInterest: function(capital,month,monthrate,payType,index){
    var _payType = payType || 1 , _index = index || 0;
    if( _payType == 1){
      var evePay = this.monthRepay(capital,month,monthrate,payType);
      return ((capital * monthrate - evePay) * Math.pow((1 + monthrate),_index) + evePay);
    }else{
      return (capital - _index * capital/ month) * monthrate;
    }
  },
  totalRepay: function(capital,month,monthrate,payType){
    var _payType = payType || 1;
    if( _payType == 1){
      var pow = Math.pow( 1 + monthrate, month);
      return capital * monthrate * pow / (pow - 1) * month;
    }else{
      return (month + 1) * capital * monthrate / 2 + capital;
    }
  },
  totalInterest: function(capital,month,monthrate,payType){
    var _payType = payType || 1;
    if( _payType == 1){
      var pow = Math.pow( 1 + monthrate, month);
      return capital * monthrate * pow / (pow - 1) * month - capital;
    }else{
      return (month + 1) * capital * monthrate / 2;
    }
  },
  capitalEvaluate: function(month,monthRepay,monthrate){
    //可贷本金 = 月还款 × [(1+月利率)^还款月数 - 1] ÷ [月利率×(1+月利率)^还款月数]
    var pow = Math.pow( 1 + monthrate, month);
    return monthRepay * (pow - 1) / (monthrate * pow);
  },
  getLoanRate: function(month,rateList){ //rateList为 3年以下,3-5年,5年以上的配置
    var rate;
    month = parseInt(month);
    if(!_.isNumber(month) && _.isNaN(month)){return}
    if(month <= 6){rate = rateList[0]}
    else if(month <= 12 && month > 6){rate = rateList[1]}
    else if(month <= 36 && month >12){rate = rateList[2]}
    else if(month <= 60 && month >36){rate = rateList[3]}
    else{rate = rateList[4]}
    return rate;
  },
  getGjjRate: function(month,rateList){ //rateList为 3年以下,3-5年,5年以上的配置
    month = parseInt(month);
    if(!_.isNumber(month) && _.isNaN(month)){return}
    return month <= 60? rateList[0] : rateList[1];
  }
};

/* 贷款30万, 120期(10年), 等额本金 */
var capital = 300000, month = 120,
  monthrate = 0.049/12,   /* 年利率4.9% */
  payType = 2;
console.log( "多还多少利息? " + m.totalInterest(capital, month, monthrate, payType).toFixed(2) ); /* 74112.50 */
console.log( "第0期应还: " + m.monthRepay(capital,month,monthrate,payType,0).toFixed(2)); /* 3725 */
console.log( "最后一期应还: " + m.monthRepay(capital,month,monthrate,payType,month-1).toFixed(2)); /* 2510.21 */
阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6