2012-03-07 65 views
0

感謝勞倫斯伯克在isMaybeMoney函數的其他問題,我能夠確定輸入是否是金錢。計算利息無限?

我現在在做的是試圖計算興趣後的總數,但我一直讓Infinity寫入屏幕。我感興趣的功能是什麼在世界上是錯誤的?如果我用1,234美元作爲3.5%利息的起始餘額,應該是3,522.55美元。

有人可以幫我嗎?

static float money; 

static void Main() 
{ 
    string[] myMaybeBalances = Accounts.GetStartingBalances(); 

    myIsMaybeMoneyValidator Miimv = new myIsMaybeMoneyValidator(); 

    ArrayList interests = Miimv.interestsAccrued(myMaybeBalances); 
    foreach (object interest in interests) 
    { 
     Console.WriteLine(interest); 
    } 

    Console.ReadLine(); 
} 

public ArrayList interestsAccrued(string[] myMaybeBalances) 
{ 
    ArrayList interests = new ArrayList(); 
    foreach (string myMaybeBalance in myMaybeBalances) 
    { 
     bool myResult = isMaybeMoney(myMaybeBalance); 
     if (myResult == true) 
     { 
      decimal[] rates = Accounts.GetRates(); 

      for (int i = 0; i < rates.Length; i++) 
      { 
       decimal rate = rates[i]; 
       float total = 1; 

       int n_X_t = 360; 
       while (n_X_t != 0) 
       { 
        rate = (1 + rates[i]/12); 
        float myRate; 
        float.TryParse(rate.ToString(), out myRate); 

        total = total * myRate; 
        total = total * money; 
        n_X_t = n_X_t - 1; 
       } 
       interests.Add(total); 
      } 
     } 
    } 
    return interests; 
} 

public bool isMaybeMoney(object theirMaybeMoney) 
{ 
    string myMaybeMoney = theirMaybeMoney.ToString(); 

    float num; 
    bool isValid = float.TryParse(myMaybeMoney, 
    NumberStyles.Currency, 
    CultureInfo.GetCultureInfo("en-US"), // cached 
    out num); 

    money = num; 
    return isValid; 
} 
+2

你爲什麼在所有的時間之間浮動和小數之間轉換?浮點數字計算不適合 - 擺脫它。 (爲什麼你不使用.NET命名約定或泛型?) – 2012-03-07 23:59:36

+0

這是幹什麼的:'total = total * money;'? – 2012-03-08 00:00:52

+0

'rates [0]'的值是多少? – 2012-03-08 01:28:22

回答

1

您通過率通過while循環,它似乎很合理的每一步乘以總的,但你也可變「錢」的價值,這是據我可以告訴的是乘總起始餘額。

所以你乘以起始餘額360倍。如果只有我的儲蓄賬戶像那樣工作!我不知道,如果邏輯的其餘部分是正確的,但一開始,嘗試移動

total = total * money; 

是線

float total = 1; 

下(或更好,但只是從

改變
float total = 1; 

float total = money; 

和克等擺脫了線

total = total * money; 

共)

+0

另外,正如@Jon Skeet提到的那樣......花些時間學習標準的C#編碼約定對你來說可能是好事,所以你的變量命名等可以讓你的代碼對其他人更容易理解。也可以使用Decimal類型,它非常適合於財務計算等,因爲它不會受到浮點計算(即使用float或double變量)時固有的不準確性的影響。 – joshuahealy 2012-03-08 00:14:09

0

你有沒有評估代碼。計算興趣構建循環的好處! 這不是要緊還引入大量風險的高併發症

這裏是代碼你想使用的職能封裝的:

static void Main() 
    { 
     var interests = new List<decimal>(); 

     foreach (string possibleBalance in Accounts.GetStartingBalances()) 
     foreach (decimal rate in Accounts.GetRates()) 
     { 
      decimal balance; 
      if (!decimal.TryParse(possibleBalance, NumberStyles.Currency, CultureInfo.CurrentCulture, out balance)) 
       continue; 

      decimal interest = CalculateInterestAccrued(balance, rate, 12, 30); 
      interests.Add(interest); 
     } 

     foreach (decimal interest in interests) 
      Console.WriteLine(interest); 

     Console.ReadKey(); 
    } 

    static decimal CalculateInterestAccrued(decimal principal, decimal rate, int compoundsPerYear, int years) 
    { 
     return principal * (decimal)Math.Pow((double)(1 + rate/compoundsPerYear), compoundsPerYear * years); 
    } 

感謝,

PRASHANT :)