2012-03-07 62 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 :)