2016-03-02 88 views
0

我想为银行的利息制作一个计算器,比如1美元需要多少年才能变成15美元,4%的利息,但是我得到的数字是一次又一次的相同数字,但我需要它像每年第一年一样走高:1美元* 4%利息,第二年:4%利息*第一年利息,第三年:4%利息*第二年利息,等等它击中$ 15银行利息计算器

private void btreikna_Click(object sender, RoutedEventArgs e) 
    { 
     double vextir4 = 0.04; 
     double vextir8 = 0.08; 
     double vextir12 = 0.12; 
     double vextir16 = 0.16; 
     double vextir20 = 0.2; 

     double startvextir = Convert.ToDouble(byrjunisk.Text); 
     double artal = Convert.ToDouble(tbartal.Text); 


     double plusplus = vextir4 * startvextir; 
     double count = artal; 

     List<int> listfullofints = new List<int>(); 

     for (int i = 0; i < artal; i++) 
     { 
      int[i]utkoma = plusplus * artal; 
     } 

回答

1

您的代码不是很清楚,但你可能想要的是这样的:

decimal target = 15; 
decimal start = 1; 
decimal interest = 0.04M; 

decimal currentCapital = start; 
var numOfYears = 0; 
while (currentCapital < target) 
{ 
    currentCapital = currentCapital + currentCapital*interest; 
    numOfYears++; 
} 

Console.WriteLine(currentCapital + " in " + numOfYears); 

几点注意事项关于那个代码和你的尝试。建议使用decimal进行精确计算(并且您希望精确计算金额:))在您的代码中,您不会更新plusplus变量 - 它始终是第一个兴趣。最后一个注释 - 你不能用于循环,因为你不会提前知道执行次数。

+0

感谢您的链接,我补充说,我的答案,相信给你。投票。 ;) – Ian

1

复利的经典公式为:

V = (1 + r)^t 

哪里V是未来值(或最终数/原号),r是利率,并且t是时间。

因此,你的情况:V = 15(从15/1),r = 0.04,发现t。或换句话说:

t = log (V)/log (1 + r) 

我推荐你用Math.Log的方法。

double t = Math.Log(15D)/Math.Log(1.04D); 

为了获得时间t你找(没有for循环)。您可能也有兴趣查看linkJleruOHeP规定的利息计算。

+1

稍微更好的公式解释:http://www.thecalculatorsite.com/articles/finance/compound-interest-formula.php – JleruOHeP