简体   繁体   English

利息计算的无穷大?

[英]Infinity in interest calculation?

Thanks to Laurence Burke in my other question for the isMaybeMoney function, I am able to determine whether an input is money or not. 多亏了Laurence Burke在我的另一个问题上,关于isMaybeMoney函数,我才能确定输入是否为金钱。

What I'm doing now is trying to calculate the total after interest but I keep getting Infinity written to the screen. 我现在正在做的是尝试计算利息后的总数,但是我一直在将Infinity写入屏幕。 What in the world is wrong with my interestsaccrued function? 我的应计利息功能到底有什么问题? It's supposed to be $3,522.55 when I use $1,234 as the starting balance with 3.5% interest. 当我使用$ 1,234作为初始余额并支付3.5%的利息时,应该是$ 3,522.55。

Can someone please help me out? 有人可以帮我吗?

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

You are multiplying total by the rate each step through the while loop, which seems reasonable enough, but you also multiply total by the value of the variable "money", which as far as I can tell is the starting balance. 您正在将合计乘以while循环中每一步的速率,这似乎已经足够合理了,但是您也将合计与变量“ money”的值相乘,据我所知,这是起始余额。

So you multiply by the starting balance 360 times. 因此,您需要用起始余额乘以360倍。 If only my savings accounts worked like that! 如果只有我的储蓄帐户能那样工作! I'm not sure if the rest of the logic is correct, but for a start, try moving the 我不确定其余逻辑是否正确,但首先请尝试移动

total = total * money;

to be under the line 在线下

float total = 1;

(or better yet just change from (或者更好的是从

float total = 1;

to

float total = money;

and get rid of the line 摆脱困境

total = total * money;

altogether) 共)

The codes you have is not evaluate. 您拥有的代码无法评估。 NO BENEFIT of construct loop for interest calculate! 计算利息的构造循环没有好处! This is not needful yet introduce much of risk for high complication 这不是必需的,但会带来很多高并发症的风险

Here is codes you want for use of FUNCTIONARY encapsulate : 这是您要使用FUNCTIONARY封装的代码:

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

thanks, 谢谢,

PRASHANT :) 代理商:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM