10

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); });

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

How can I change this messages?

Edit

Calling code:-

function performCalc() {
  CalculatePMTFromForm('LoanAmount', 'InterestRate', 'Months', 'Payment');
}

jQuery(document).ready(function() {
  performCalc(); jQuery('.calc').keyup(performCalc);
});

This is worked for me:

if(pmt>0 && pmt<Number.MAX_VALUE) {jQuery('#' + idResult).val(pmt.toFixed(2));}
Holian
  • 101
  • 1
  • 1
  • 4
  • Show us the call to CalculatePMTFromForm? – AnthonyWJones Jan 19 '10 at 16:25
  • function performCalc() { CalculatePMTFromForm('LoanAmount', 'InterestRate', 'Months', 'Payment'); } jQuery(document).ready(function() { performCalc(); jQuery('.calc').keyup(performCalc); }); – Holian Jan 20 '10 at 08:41
  • And you are sure a) that each of the INPUT elements in question have the correct ID (and that ID is not duplicated) and b) the each INPUT has an initial value provided by the server. – AnthonyWJones Jan 20 '10 at 10:22
  • a) YES, b) No initial value, but if i set 0 for example, then NaN is disappear. But user can delete the 0 value from input box, so NaN is shown up again. Somehow i need to change this message, or i have to check if the result input field value is NaN, then i have to give another value..just i dont know how.. – Holian Jan 20 '10 at 11:13
  • Hi can any one tell me what post is the answer for this question.. i m reading all the post but not able to understannd which one will be the answer to implement in my project. – Murtaza Jan 10 '12 at 04:39

3 Answers3

36

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 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.

Adam
  • 2,851
  • 1
  • 20
  • 20
0

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()

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • i dont really understand. Even if i check the input fields validity, the user can write wrong format, just get a msg to type "number or smtng"...But till the user can tpye wrong character then get a message in result input field NaN. I will check the input field, but i need to turn of somehow the NoN and Infinity message. – Holian Jan 20 '10 at 08:55
0

Try it like this:-

function CalculatePMTFromForm(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.

AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306