Excel Pmt Function In Js
Solution 1:
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:
functionpmt(rate_per_period, number_of_payments, present_value, future_value, type){
if(rate_per_period != 0.0){
// Interest rate existsvar 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)));
} elseif(number_of_payments != 0.0){
// No interest rate, but number of payments existsreturn -(future_value + present_value) / number_of_payments;
}
return0;
}
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%
).
An example:
/* 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);
And the payment for the example period (month) is ~$474.60.
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.
Solution 2:
NaN means "Not A Number".
Make sure you check for each input value whether it is numeric. Throw an error message if one is not, or set it to 0, depending on whether it's essential to your calculation or not.
A good collection of the best ways to check a value for whether it's numeric is this SO question: Validate numbers in JavaScript - IsNumeric()
Solution 3:
Try it like this:-
functionCalculatePMTFromForm(idLoanAmount, idAnnualInterestRate, idMonths, idResult) {
var i = parseFloat($('#' + idAnnualInterestRate).val()) / 1200;
var n = parseFloat($('#' + idMonths).val());
var p = parseFloat($('#' + idLoanAmount).val());
var pmt = PMT(i, n, -p);
$('#' + idResult).val(pmt.toFixed(2));
}
The .val() is likely returning a string type not a number type.
Post a Comment for "Excel Pmt Function In Js"