JS中的Excel PMT功能 [英] Excel PMT function in JS
问题描述
我在互联网上找到了关于PMT计算的小snipet。
I found i little snipet on internet, about PMT calculate.
function PMT(i, n, p) {
return i * p * Math.pow((1 + i), n) / (1 - Math.pow((1 + i), n));
}
function CalculatePMTFromForm(idLoanAmount, idAnnualInterestRate, idMonths, idResult) {
var i = jQuery('#' + idAnnualInterestRate).val() / 1200;
var n = jQuery('#' + idMonths).val();
var p = jQuery('#' + idLoanAmount).val();
var pmt = PMT(i, n, -p);
jQuery('#' + idResult).val(pmt.toFixed(2));
}
function performCalc() {
CalculatePMTFromForm('LoanAmount', 'InterestRate', 'Months', 'Payment');
}
jQuery(document).ready(function() { performCalc(); jQuery('.calc').keyup(performCalc); });
当页面加载时,在结果输入框中我看到NaN,当我tpye一些不相关的数字然后出现-Infinitymsg。
我在文件中搜索NaN,我在jquery.js中找到,但是在我修改之后,没有任何改变。我找不到Infinity
When the page is load, in the result input box I see "NaN" , and when i tpye some irrelevant number then "-Infinity" msg appear. I search to "NaN" in files and i found in jquery.js, but after I modify, nothing change. And I can't find Infinity
如何更改此消息?
编辑
致电代码: -
function performCalc() {
CalculatePMTFromForm('LoanAmount', 'InterestRate', 'Months', 'Payment');
}
jQuery(document).ready(function() {
performCalc(); jQuery('.calc').keyup(performCalc);
});
这对我有用:
if(pmt>0 && pmt<Number.MAX_VALUE) {jQuery('#' + idResult).val(pmt.toFixed(2));}
推荐答案
这个问题已经死了一年多了,但我最近需要这样做一样。这是我想出的:
This question's been dead for over a year, but I recently needed to do the same thing. Here's what I came up with:
function pmt(rate_per_period, number_of_payments, present_value, future_value, type){
if(rate_per_period != 0.0){
// Interest rate exists
var q = Math.pow(1 + rate_per_period, number_of_payments);
return -(rate_per_period * (future_value + (q * present_value))) / ((-1 + q) * (1 + rate_per_period * (type)));
} else if(number_of_payments != 0.0){
// No interest rate, but number of payments exists
return -(future_value + present_value) / number_of_payments;
}
return 0;
}
type
需要是 1
或 0
,与Excel相同。 rate_per_period
需要是小数(例如: 0.25
,而不是 25%
)。
type
needs to be 1
or 0
, same as Excel's. The rate_per_period
needs to be a decimal (eg: 0.25
, not 25%
).
一个例子:
/* Example: */
var interest = 0.07, // Annual interest
years = 5, // Lifetime of loan (in years)
present = 10000, // Present value of loan
future = 20000, // Future value of loan
beginning = 1; // Calculated at start of each period
var payment = -pmt(interest / 12, // Annual interest into months
years * 12, // Total months for life of loan
present,
future,
beginning);
示例期间(月)的付款为$ 474.60。
And the payment for the example period (month) is ~$474.60.
注意结果的否定,因为金额是一个dedection - 即:花费474美元 - 结果是负值。如果结果是信用,结果将是积极的。通常你会想把它保持为负面/正面,但如果你以总债务:$ XYZ
的格式显示它,你想要转换它积极的。
Note the negation of the result, as the amount is a dedection - ie: costs you $474 - the result is a negative value. Were the result to be a credit, the result would be a positive. Generally you'll want to keep it as a negative/positive, but if you were displaying it in a format like Total Debt: $XYZ
, you'd want to convert it to a positive.
这篇关于JS中的Excel PMT功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!