如何在Javascript中准确计算每日,每周,每月复利?

时间:2022-05-12 12:37:18

I need a very accurate calculator formula. Right now my formula works for monthly contributions with monthly compounding, but when you use weekly contributions with monthly compounding, I don't know how to adjust the formula. You can see my calculator in action by downloading and unzipping the attached files.

我需要一个非常准确的计算器公式。现在我的公式适用于按月复利的每月供款,但是当你使用每月供款和每月复合时,我不知道如何调整公式。您可以通过下载和解压缩附加文件来查看我的计算器。

So far, this is the most accurate calculator I've found on the web. I'd be happy if I could get a Javascript formula to match the values generated by this calculator.

到目前为止,这是我在网上找到的最准确的计算器。如果我能得到一个Javascript公式来匹配这个计算器生成的值,我会很高兴。

The problem with weekly contributions and monthly compounding is that while there are 52 weeks in a year, some months have 5 weeks and others have 4. I don't think their calculator takes this into account; I think they simply base their calculation on 52 weeks and every 4 weeks they compound the interest.

每周捐款和每月复利的问题是,虽然一年有52周,有些月有5周,有些有4个。我不认为他们的计算器会考虑到这一点;我认为他们只是将他们的计算基于52周,而且每4周就会使他们感兴趣。

Here is the formula I'm using:

这是我正在使用的公式:

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;

Given a scenario of

鉴于一种情况

  • $1000 principal,
  • $50 per week contribution,
  • 每周50美元的捐款,

  • 10 years,
  • monthly compounding,

the other calculator says total should be $30,007, rounding the decimals up. The closest I've come to this is using this formula is by weekly contributions and weekly compounding (But I want monthly compounding!):

另一个计算器说总计应该是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);

How can I make my formula work for weekly and daily contributions?

如何使我的配方工作为每周和每日捐款?

1 个解决方案

#1


Well... Both of these are slightly off from that calculator, but possibly more accurate to how things are actually done (not sure, however, because I don't know how it is actually done):

嗯......这两个都略微偏离了那个计算器,但可能更准确地说明事情是如何完成的(但不确定,因为我不知道它是如何实际完成的):

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

#1


Well... Both of these are slightly off from that calculator, but possibly more accurate to how things are actually done (not sure, however, because I don't know how it is actually done):

嗯......这两个都略微偏离了那个计算器,但可能更准确地说明事情是如何完成的(但不确定,因为我不知道它是如何实际完成的):

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