javascript - Javascript 中的 PMT 函数

标签 javascript excel excel-formula javascript-framework

我想在 Javascript 中使用 Excel PMT 函数。 参数将是

Pmt( interest_rate, number_payments, PV, FV, Type )

interest_rate : the interest rate for the loan.
number_payments : the number of payments for the loan.
PV : the present value or principal of the loan.
FV : It is the future value or the loan amount outstanding after all payments have been made. 

Type is : It indicates when the payments are due. Type can be one of the following values:
 0, 1

您可以引用: http://www.techonthenet.com/excel/formulas/pmt.php

这是我使用的代码,我卡在了最后一个参数中。 “类型是”0 或 1。请问它如何影响计算。

function PMT (ir, np, pv, fv ) {
 /*
 ir - interest rate per month
 np - number of periods (months)
 pv - present value
 fv - future value (residual value)
 */
 pmt = ( ir * ( pv * Math.pow ( (ir+1), np ) + fv ) ) / ( ( ir + 1 ) * ( Math.pow ( (ir+1), np) -1 ) );
 return pmt;
}

我需要纯 Javascript 而不是 jQuery。

最佳答案

这是经过谷歌搜索后我的 PMT 函数版本:

function PMT(ir, np, pv, fv, type) {
    /*
     * ir   - interest rate per month
     * np   - number of periods (months)
     * pv   - present value
     * fv   - future value
     * type - when the payments are due:
     *        0: end of the period, e.g. end of month (default)
     *        1: beginning of period
     */
    var pmt, pvif;

    fv || (fv = 0);
    type || (type = 0);

    if (ir === 0)
        return -(pv + fv)/np;

    pvif = Math.pow(1 + ir, np);
    pmt = - ir * (pv * pvif + fv) / (pvif - 1);

    if (type === 1)
        pmt /= (1 + ir);

    return pmt;
}

例子 以 7.5% 的年利率在 15 年内还清 200,000 美元的贷款需要每月还款多少?

ir = 0.075 / 12
np = 15 * 12
pv = 200000
pmt = PMT(ir, np, pv).toFixed(2) = -1854.02
payoff = pmt * np = -333723.6

关于javascript - Javascript 中的 PMT 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5294074/

相关文章:

vba - FileSystemObject.CreateFolder 创建目录和子目录

excel - 如何选择 'or' 值?

excel - 两个日期之间的天数,条件为空白输入

excel-formula - Excel公式删除单元格中单词之间的空格

JavaScript object.hasOwnProperty(proName) vs lodash _.has(obj, proName) 函数

javascript - 为什么 Javascript 不验证我的 html 代码?

javascript - Karma 错误 - 没有捕获浏览器,打开 http ://localhost:9876/

excel - Selenium Webdriver (VBA) - 查找元素的属性,其中有相同名称的重复属性

arrays - Excel数组查找公式

javascript - React Redux 与多个 applyMiddleware