JavaScript利息贷款计算器算法

它使用百分比利息的计算方法,该对数方法使用对数来计算本金付款/总利息,以及在给定借款金额和每月还款金额的情况下还清贷款的时间

//FORMULA FOR CALUCLATING INTEREST RATE
//p = x*(1 - (1+r)^-n)/r

var interest = 15, rate = interest/100, principal = 1000, payment = 100, noofpay = 12;

var nper1 = Math.log((1-((principal/payment) * (rate/noofpay))));
var nper2 = Math.log((1+(rate/noofpay)));
nper = -(nper1/nper2);
interestpaid=payment*nper-principal;
nper = -Math.round((nper1/nper2));
nyear=Math.floor(nper/12);
nmonth=nper%12;
if (nper>0)
{
    if (nmonth==0)
    {
        period=nyear+" Year(s)";
    }
    else
    {
        period=nyear+" Year(s) and "+nmonth+" Month(s)";
    }
}
else
{
    period=" Invalid Values ";
    interestpaid=0;
}

console.log("Montly Payments: " + period + ", Total Interest Paid: " + interestpaid.toFixed(2));

传入参数的函数:

//the price calculation formula
//@return the price and length of time
function calculate(interest, principal, payment)
{
    //get data
    var calcElem = $('#calc');

    //FORMULA FOR CALUCLATING INTEREST RATE
    //p = x*(1 - (1+r)^-n)/r

    var rate = interest/100, noofpay = 12;

    var nper1 = Math.log((1-((principal/payment) * (rate/noofpay))));
    var nper2 = Math.log((1+(rate/noofpay)));
    nper = -(nper1/nper2);
    interestpaid=payment*nper-principal;
    nper = -Math.round((nper1/nper2));
    nyear=Math.floor(nper/12);
    nmonth=nper%12;
    if (nper>0)
    {
        if (nmonth==0)
        {
            period=nyear+" Year(s)";
        }
        else
        {
            period=nyear+" Year(s) and "+nmonth+" Month(s)";
        }
    }
    else
    {
        period=" Invalid Values ";
        interestpaid=0;
    }

    //console.log("Montly Payments: " + period + ", Total Interest Paid: " + interestpaid.toFixed(2));

    var priceData = {
        price : '$'+this.formatCurrency(interestpaid+owe),
        interest : '$'+this.formatCurrency(interestpaid),
        time  : period
    }
    return priceData;
}

From: https://www.sitepoint.com/javascript-interest-loan-calculator-algorithm/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值