有利息计算问题(包括测试)

时间:2012-03-19 21:23:50

标签: c# currency

感谢大家解决我从上一个问题中得到的无限问题:

Infinity in interest calculation?

最近虽然我转发了来自客户的电子邮件,抱怨他们最终得到的数字没有正确加起来 - 但我不知道这是怎么可能的。我自己使用我在下面的测试应用程序中输入的值来测试这个,但我只是没有看到问题 - 根据我在输入值时使用的其他兴趣程序,结果正确($ 352.24) 。我不确定在哪里可以看,有人可以帮我弄清楚这里发生了什么吗?

以下是我正在测试的修改后的代码:

static float money;

static void Main()
{
    //string[] myMaybeBalances = Accounts.GetStartingBalances();
    string[] myMaybeBalances = new string[1];
    myMaybeBalances[0] = "123.45";

    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();
            decimal[] rates = new decimal[1];
            rates[0] = (decimal)0.0350;

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

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

修改

我尝试将其更改为

rate = (1 + rates[i] / 12.0); 

当我点击绿色播放按钮时出现此错误:

错误1运算符'/'无法应用于'decimal'和'double'类型的操作数C:\ Users \ dave \ Documents \ Visual Studio 2010 \ Projects \ ConsoleApplication2 \ ConsoleApplication2 \ Program.cs 74 41 ConsoleAp plication2

1 个答案:

答案 0 :(得分:0)

此字符串rate = (1 + rates[i] / 12);看起来很可疑。如果除以整数(12),结果也将是整数。尝试将其更改为rate = (1 + rates[i] / 12.0);