Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Interest Paid IPMT Excel equivelant

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 02-17-2013, 03:35 AM PM User | #1 billboy New Coder   Join Date: Feb 2013 Posts: 44 Thanks: 2 Thanked 0 Times in 0 Posts Interest Paid IPMT Excel equivelant Does anyone have the formula or script to calculate the remaing interest left on a loan? Using JS of course Thanks
 02-17-2013, 03:48 AM PM User | #2 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,952 Thanks: 75 Thanked 4,307 Times in 4,274 Posts Interest only? Not the principal? Or principal + interest? Most loans nowadays don't have prepayment penalties, so if you are ready to pay one off, you only owe the remaining principal. So I'm not sure your question, as given, makes sense. The title of your query made more sense: How much interest has been *paid* on a loan, at a given point in time? That's a sensible question. __________________ An optimist sees the glass as half full. A pessimist sees the glass as half empty. A realist drinks it no matter how much there is.
 02-17-2013, 04:24 AM PM User | #3 billboy New Coder   Join Date: Feb 2013 Posts: 44 Thanks: 2 Thanked 0 Times in 0 Posts It is not for payoff, I want to calculate the remaing interest that will be paid on a loan if all remaing scheduled payments are made
 02-17-2013, 04:42 AM PM User | #4 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,952 Thanks: 75 Thanked 4,307 Times in 4,274 Posts Okay...not too hard. The basic formula for finding the payment amount on a loan is: Code: `PMT = PV / ( ( 1 - Math.pow( 1 + RATE, - PERIODS) ) / RATE );` Where PMT is the payment, PV is the PresentValue (the loan amount), PERIODS is the number of periods (month, weeks, years, whatever) and RATE is the interest rate per PERIOD (that is, monthly rate if PERIODS are months) expressed as a fraction. So, for example: Code: ```PV = 150000; YEARS = 30; PERIODS = YEARS * 12; APR = 4.25; // percent RATE = 4.25 / 12 / 100; // convert APR to monthly rate PMT = PV / ( ( 1 - Math.pow( 1 + RATE, - PERIODS) ) / RATE ); document.write(PMT);``` (Not good JavaScript; strictly for demo purposes.) To find the interest paid over the life of a loan is simple: Code: `TOTALINTEREST = ( PMT * PERIODS ) - PV;` That is, the total of payments less the original loan amount. Now, the beauty of the payment formula is that you can turn it around: Code: `PV = PMT * ( ( 1 - Math.pow( 1 + RATE, - PERIODS) ) / RATE );` That is, given the payemt, rate, and periods, you can calculate the loan amount. So say that you have paid 10 years on a 30 year loan. Clearly, there are still 20 years, or 240 months, still to go. So to find out the PresentValue of the loan, with 240 months to go, you just plug in the PMT value you got from the first calculation and change the PERIODS to 240 and calculate the PV. Then, again, you get the interest paid over those next 20 years by subtracting the PV from the total of payments. __________________ An optimist sees the glass as half full. A pessimist sees the glass as half empty. A realist drinks it no matter how much there is.
 Users who have thanked Old Pedant for this post: billboy (02-19-2013)
 02-17-2013, 04:59 AM PM User | #5 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,952 Thanks: 75 Thanked 4,307 Times in 4,274 Posts Try this: Code: ``` Remaining interest on a loan
Original loan amount:
Number of months of loan:
Annual interest rate:
Number of months remaining on loan:

For life of loan:
• Total of payments: \$
• Total interest: \$
Remaining on loan:
• Total of payments: \$
• Total interest: \$
``` **** EDIT: Change the ID of the to keep Philip and Felgall happy. __________________ An optimist sees the glass as half full. A pessimist sees the glass as half empty. A realist drinks it no matter how much there is. Last edited by Old Pedant; 02-17-2013 at 11:44 PM..
 02-17-2013, 05:03 AM PM User | #6 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,952 Thanks: 75 Thanked 4,307 Times in 4,274 Posts You can try this example: Original loan amount: 2124.33 Number of months of loan: 24 Annual interest rate: 12 Number of months remaining on load: 3 And that happens to be one that I worked out almost 40 years ago and it's the right answer. __________________ An optimist sees the glass as half full. A pessimist sees the glass as half empty. A realist drinks it no matter how much there is.
 02-17-2013, 01:53 PM PM User | #7 Philip M Supreme Master coder!     Join Date: Jun 2002 Location: London, England Posts: 17,481 Thanks: 200 Thanked 2,470 Times in 2,448 Posts May I point out that you have assigned the same word "loan" to the form id and the name of an input field (the original loan amount)? That seems to me to be a recipe for confusion. And presumably the number of months must be integer. All input values must be positive, so I would suggest use Math.abs() __________________ All the code given in this post has been tested and is intended to address the question asked. Unless stated otherwise it is not just a demonstration. Last edited by Philip M; 02-17-2013 at 01:59 PM..
 02-17-2013, 10:37 PM PM User | #8 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,952 Thanks: 75 Thanked 4,307 Times in 4,274 Posts Ehhh...it doesn't look like he cares about the answer, so I don't think I will much, either. The validation was purposely weak (see the comment only re use of alert), as I didn't want to spend much time on it. Not knowing what kind of loans he is talking about, I'd probably also want to restrict the interest rate to a reasonable range (e.g., so somebody doesn't put in 0.055 in place of 5.5%). I don't think I agree with using Math.abs(). I would rather reject a negative value, on the basis that the - may have been a typo. I know your objection to reusing a name, but an id for a
and a name for an are unlikely to be confused, as they won't be used in the same way. But yes, I should have called it "loanForm" or such, instead. __________________ An optimist sees the glass as half full. A pessimist sees the glass as half empty. A realist drinks it no matter how much there is.
02-17-2013, 10:43 PM   PM User | #9
felgall
Master Coder

Join Date: Sep 2005
Location: Sydney, Australia
Posts: 6,094
Thanks: 0
Thanked 579 Times in 569 Posts
Quote:
 Originally Posted by Old Pedant I know your objection to reusing a name, but an id for a
and a name for an are unlikely to be confused, as they won't be used in the same way.
However versions of Internet Explorer that pollute the id namespace with name attributes will potentially confuse the two.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/

Beginners need to advise whether they want to learn "Latin" JavaScript for Netscape 3 or "Italian" JavaScript for modern browsers.

02-17-2013, 11:43 PM   PM User | #10
Old Pedant
Supreme Master coder!

Join Date: Feb 2009
Posts: 24,952
Thanks: 75
Thanked 4,307 Times in 4,274 Posts
Quote:
 Originally Posted by felgall However versions of Internet Explorer that pollute the id namespace with name attributes will potentially confuse the two.
Hasn't been my experience.

If an element is given an ID, then that overrides the name-promoted-to-id in every case I have seen.

Don't forget, I still have hundreds of page hits from MSIE 7 and below (even hundreds of MSIE 5.5!) every week. And though I don't have many cases where I have an ID and NAME that are the same, I'm sure there are some.

Yes, if you have two elements with the same NAME and no element with that ID, then older MSIE will get confused. But that's not the same as one with ID and one with NAME.

Not saying it can't happen. Just that I haven't seen it happen.

But okay, I'll keep you and Philip happy. I changed the ID in my code to "loanForm".
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.

02-18-2013, 02:52 AM   PM User | #11
felgall
Master Coder

Join Date: Sep 2005
Location: Sydney, Australia
Posts: 6,094
Thanks: 0
Thanked 579 Times in 569 Posts
Quote:
 Originally Posted by Old Pedant If an element is given an ID, then that overrides the name-promoted-to-id in every case I have seen.
I was thinking in more general terms rather than your specific code.

That the id overrides the name would have the potential to confuse someone who saw the HTML with the name and was expecting to be able to reference it directly the way IE allows only to find that the variable they were trying to use actually points to an id elsewhere in the HTML that they didn't even notice until their code didn't work.

Not that I am suggesting using those variables anyway - they only really give an additional reason to wrap code inside anonymous functions so that you don't get accidental clashes with the extra variables that IE creates.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/

Beginners need to advise whether they want to learn "Latin" JavaScript for Netscape 3 or "Italian" JavaScript for modern browsers.

02-18-2013, 08:47 AM   PM User | #12
Philip M
Supreme Master coder!

Join Date: Jun 2002
Location: London, England
Posts: 17,481
Thanks: 200
Thanked 2,470 Times in 2,448 Posts
Quote:
 Originally Posted by Old Pedant But okay, I'll keep you and Philip happy. I changed the ID in my code to "loanForm".
I am sure we have discussed this before.

Code:
```<input id = "text">

<script type = "text/javascript">
text = "Hello World";  // global variable
document.getElementById('text').value = text;
</script>```
Does not work in IE9 (or any other version).

To me the solution is simple. Never, ever, duplicate HTML names/ids/Javascript variable names. Also make the names descriptive ("loanamount", not "la"or "xyz").

Although a global variable is not the same as a local variable of the same name, it is bound to create confusion, and is simply pointless. The days when the names of variables were limited to a single letter have long gone!

IMO the name of an HTML element and the id of that element should be the same word. But as you say, there is often no point in assigning ids to form elements.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 02-18-2013 at 08:51 AM..

 02-18-2013, 05:10 PM PM User | #13 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,952 Thanks: 75 Thanked 4,307 Times in 4,274 Posts No disagreement, Philip. You see any place in my code where I have a variable name the same as an element id or name? This is a different case: Having an id of one element that is the same as the name of another. NOT trying to say it's a good idea, just that it doesn't cause conflicts. But in any case, I changed the code. __________________ An optimist sees the glass as half full. A pessimist sees the glass as half empty. A realist drinks it no matter how much there is.
 02-19-2013, 07:56 AM PM User | #14 billboy New Coder   Join Date: Feb 2013 Posts: 44 Thanks: 2 Thanked 0 Times in 0 Posts Thanks
 02-20-2013, 12:04 AM PM User | #15 007julien Regular Coder   Join Date: May 2012 Location: France Posts: 174 Thanks: 0 Thanked 27 Times in 25 Posts See this page to translate in English

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 12:12 PM.