2009-04-15 41 views
3

非常に正確な計算式が必要です。今の私の数式は毎月の配合で毎月の寄付に役立ちますが、あなたは毎月の配合で毎週の寄付を使用するとき、私は数式を調整する方法がわかりません。添付ファイルをダウンロードして解凍することで、私の電卓を実際に見られます。Javascriptで毎日、毎週、毎月の複利計算を正確に計算するにはどうすればよいですか?

これまでのところ、this is the most accurate calculator私はウェブ上で見つけました。この電卓で生成された値に合わせてJavascriptの式を得ることができれば幸いです。

毎週の寄付と毎月の配合に関する問題は、1年に52週間ありますが、いくつかの月には5週間あり、他には4があります。私の計算ではこれを考慮していないと思います。私は、彼らが52週間で計算し、4週間ごとに関心を集めていると考えています。

  • $ 1000校長、
  • 週間の貢献、
  • 10年ごとの$ 50
  • のシナリオを考えると
    var P = startingAmount; 
    var Y = yearsOfInvesting; 
    var c = getAdditionalContributionsPerPeriod; 
    var t = getTermsPerPeriod; 
    var n = t * Y; 
    var r = interestRate/100/t; // interestRate [%] 
    var z = 1 + r; 
    total = P * Math.pow(z, n) + c * (Math.pow(z, n + 1) - z)/r; 
    

    を:ここで

    は、私が使用している式であります毎月の配合、

他の電卓は、totalは$ 30,007で、小数点以下を四捨五入します。私はこれに来て最も近いが、この式を使用して、毎週の貢献と毎週の配合である(しかし、私は毎月の配合をしたい!):

var P = 1000;//startingAmount; 
var Y = 0;//yearsOfInvesting; 
var c = 50;// 
var n = 520;//t * Y; 
var r = .02/52; 
var z = 1 + r; 

mz = P * Math.pow(z, n) + c * (Math.pow(z, n + 1) - z)/r; 
document.write(mz); 

どのように私は、毎週、毎日の貢献のための私の式の作業を行うことができますか?

答えて

1

うーん...これらの両方は、(私はそれが実際にどのように行われるかわからないので、しかし、わからない)可能性が物事が実際に行われているかをより正確にその計算から少しずれていますが、:

function calc(startingAmount, yearsOfInvesting, additionalContributionsPerPeriod, interestRate) { 
    var interestPerDay = (interestRate/365); 
    var total = startingAmount; 
    var date = new Date(new Date().getFullYear() + new Date().getMonth() + 1, 1); 
    var endDate = new Date(date.getFullYear() + yearsOfInvesting, date.getMonth(), date.getDate() - 1); 
    var startingWeekday = date.getDay(); 
    var startingDate = date.getDate(); 
    var runningInterest = 0; 
    while(Date.parse(date.toString()) < Date.parse(endDate.toString())) { 
     date.setDate(date.getDate() + 1); 
     runningInterest = runningInterest + total * interestPerDay; 
     if(date.getDay() == startingWeekday) { 
      total = total + additionalContributionsPerPeriod; 
     } 
     if(date.getDate() == startingDate) { 
      total = total + runningInterest; 
      runningInterest = 0; 
     } 
    } 
    total = total + runningInterest; 
    return total; 
} 
function calc2(startingAmount, yearsOfInvesting, additionalContributionsPerPeriod, interestRate) { 
    var interestPerDay = (interestRate/365); 
    var total = startingAmount; 
    var runningInterest = 0; 
    for(var day = 1; day <= 365 * yearsOfInvesting; day++) { 
     runningInterest = runningInterest + total * interestPerDay; 
     if(day % 7 == 0) { 
      total = total + additionalContributionsPerPeriod; 
     } 
     if(day % 30 == 0) { 
      total = total + runningInterest; 
      runningInterest = 0; 
     } 
    } 
    total = total + runningInterest; 
    return total; 
} 
document.write(3647 + "<br>" + calc(1000, 1, 50, 0.02) + "<br>" + calc2(1000, 1, 50, 0.02)); 
document.write("<br><br>"); 
document.write(6347 + "<br>" + calc(1000, 2, 50, 0.02) + "<br>" + calc2(1000, 2, 50, 0.02)); 
document.write("<br><br>"); 
document.write(14779 + "<br>" + calc(1000, 5, 50, 0.02) + "<br>" + calc2(1000, 5, 50, 0.02)); 
document.write("<br><br>"); 
document.write(30007 + "<br>" + calc(1000, 10, 50, 0.02) + "<br>" + calc2(1000, 10, 50, 0.02)); 
document.write("<br><br>"); 
document.write(31673 + "<br>" + calc(1000, 10, 50, 0.03) + "<br>" + calc2(1000, 10, 50, 0.03)); 
document.write("<br><br>"); 
document.write(33460 + "<br>" + calc(1000, 10, 50, 0.04) + "<br>" + calc2(1000, 10, 50, 0.04)); 
document.write("<br><br>"); 
document.write(35378 + "<br>" + calc(1000, 10, 50, 0.05) + "<br>" + calc2(1000, 10, 50, 0.05)); 
document.write("<br><br>"); 
document.write(772849953 + "<br>" + calc(1000, 55, 50, 0.20) + "<br>" + calc2(1000, 55, 50, 0.20)); 
関連する問題