2016-03-02 89 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