Skip to content

Instantly share code, notes, and snippets.

@ghalimi
Created January 23, 2013 23:13
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ghalimi/4615572 to your computer and use it in GitHub Desktop.
Save ghalimi/4615572 to your computer and use it in GitHub Desktop.
NPER Function
// Copyright (c) 2012 Sutoiku, Inc. (MIT License)
function NPER(rate, payment, present, future, type) {
// Initialize type
var type = (typeof type === 'undefined') ? 0 : type;
// Initialize future value
var future = (typeof future === 'undefined') ? 0 : future;
// Evaluate rate and periods (TODO: replace with secure expression evaluator)
rate = eval(rate);
// Return number of periods
var num = payment * (1 + rate * type) - future * rate;
var den = (present * rate + payment * (1 + rate * type));
return Math.log(num / den) / Math.log(1 + rate);
}
@insightcoder
Copy link

Doesn't seem to work when rate is 0

@payneal
Copy link

payneal commented Aug 15, 2018

thanks man your a life saver

@musman92
Copy link

try passing

NPER(0/12, 200, -3000, 0)
this function fails

@mnoushadmhd
Copy link

i have rate: 3.25% ,payment:5000,present:150000,
why does it return a negative value . How can i get no of months from this or the amount with interest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment