Interest calculations

1. Senior Member
Join Date
Dec 1969
Posts
359

## Interest calculations

Hope somebody could help me.<BR>Lets say I have an investment of \$10,000.00 now at a yearly interest rate of of 10% i get back \$1,000.00 now lets say I have to show the interest that acumulated "to date" so if i looked at clients account 5 months and 20 days into the year how can I calculate that in javascript.<BR>I&#039;m already doing the calculations for 1,2,3 years I need to do it to date here is my code if it can be used.<BR><BR>function calculate(t){<BR>var frm = document.investdata;<BR>var years;<BR>var re = /[^0-9-.]/g ;<BR>var invamnt = parseFloat( frm.reqamount.value.replace(re,"") ); <BR>var intrate = frm.reqinterestrate.value;<BR>var comm = parseFloat(frm.reqcommval.value);<BR>var val = t.options[t.selectedIndex].value;<BR><BR> switch(val){ <BR> case "12":<BR> years = &#039;1&#039;<BR> break;<BR> case "24": <BR> years = &#039;2&#039;<BR> break;<BR> case "36":<BR> years = &#039;3&#039;<BR> break;<BR> }<BR> var commtotal = Math.round((invamnt*(Math.pow((1+comm/100),years))-invamnt)*100)/100<BR> var inttotal = Math.round((invamnt*(Math.pow((1+intrate/100),years))-invamnt)*100)/100<BR> var mattotal = Math.round(invamnt*(Math.pow((1+intrate/100),years))*100)/100;<BR> frm.maturityamount.value = formatCurrency(mattotal);<BR> frm.interestamount.value = formatCurrency(inttotal);<BR> frm.commamount.value = formatCurrency(commtotal);<BR>}<BR>

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## Why do you use a string...

...for the years value (in that switch) only so that JS can convert it to a number in the calculations?<BR><BR>Why not just assign a *number* in the first place?<BR><BR>And why do you round the totals to whole dollars??? That&#039;s not realistic in investment banking.<BR><BR>************<BR><BR>Anyway, the answer is easy:<BR><BR>Divide the commission rate (comm/100, that is, and why do you calculate that 3 separate places?) by the number of days in the year. With reasonable accuracy, you can use 365.25 for the number of days. If you need absolute accuracy, then you&#039;d have to figure the number of days in the particular year in question.<BR><BR>So something like:<BR> var dailyComm = 1.0 + comm/( 100 * 365.25 );<BR> var commtotal = invamnt * ( Math.pos(dailyComm,numberOfDays) - 1.0 );<BR><BR>You can calculate the number of days by using getTime() [think that&#039;s the name] to get milliseconds, subtracting to get diff in milliseconds, then dividing by 1000*60*60*24 to get days.<BR><BR>Depending on how accurate this needs to be, you might want to first get the accumulated commission for a whole number of years (using basically what you have now) and then only get the additional portion of a year using the daily rate scheme.<BR>

3. Senior Member
Join Date
Dec 1969
Posts
359

## Thanks

Great thanks

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•