1

i am still new to learning C# and was wondering if i could get some help. I am writing a program C# and Windows forms so that users can calculate their monthly payments and interest for a mortgage. The equation i have for the payments is: Payment = p * r / ( 1 - ( 1 + r ) ^ ( -n ) ) Where p is the amount of the loan, r is the monthly interest rate given as a number from 0 (for 0 percent) and 1 (for 100 percent), n is the duration of the loan in months Then the formula for the total interest paid is: total interest = n * payment –p

Now i have tried entering all of these numbers as doubles using the Math.Pow method for the payments and got incorrect calculations. I am assuming that the rate NEEDS to be a decimal, so when i try them ALL as decimals, VS doesnt like the "^" method or the math.pow method. So my question is, how are you supposed to use decimals with exponents? For those that wish to see my current code please note that i am just trying to get the calculations finished before i start adding extra 'else' statements.

        decimal amnt = Convert.ToDecimal(txtAMNT.Text);
        string Amount=Convert.ToString(txtAMNT.Text);
        decimal rate = Convert.ToDecimal(txtRATE.Text);
        string Rate = Convert.ToString(txtRATE.Text);
        decimal time = Convert.ToDecimal(txtTIME.Text);
        string Time=Convert.ToString(txtTIME.Text);
        decimal monthpay;
        decimal totalinterest;
        decimal realrate = rate / 100;

        if ((Amount == "")||(Rate == "")||(Time==""))
        {
            MessageBox.Show("Please fill all boxes with numbers");
        }
        else
        {
            monthpay=amnt*realrate/(1-(1+realrate)^(-time));
                totalinterest=time*monthpay-amnt;
            mtbMonPay.Text=monthpay.ToString("c");
            mtbTotalInterest.Text=totalinterest.ToString("c");
        }
SprJD7903
  • 105
  • 1
  • 10
  • Why are you `Convert.ToString`ing things that are already Strings (e.g. `txtAMNT.Text`)? – Sam Axe Mar 22 '15 at 04:13
  • `Decimal.TryParse` is usually considered better than `Convert.ToDecimal`. – Sam Axe Mar 22 '15 at 04:15
  • idk why, its just been the way i have been writing some of my lines, i know that its already a string, but i guess its what i was taught i guess. idk i do it that way the most of that time. Regardless that shouldn't have an impact on the math part of the code. – SprJD7903 Mar 22 '15 at 04:18
  • sam, thats interesting, the tryparse method allows me to make it as a string and a decimal? i like that lol, but then how would the VB be able to tell the difference between them as a string and as a decimal? – SprJD7903 Mar 22 '15 at 04:20
  • What are your input values, what are your output values, and what are the output values you are expecting? – phoog Mar 22 '15 at 04:24
  • Also, the code you wrote it C#, not VB. – Sam Axe Mar 22 '15 at 04:26
  • @phoog - With the amnt = $100.00, rate=5.00%, time(months)=24. These inputs should equal monthpay=$4.39, and totalinterest=$5.29. This was an example that was provided to me. – SprJD7903 Mar 22 '15 at 04:28
  • Sorry i meant VS (Visual Studios) ill change it thanks Sam – SprJD7903 Mar 22 '15 at 04:29
  • By doesn't like the `^`, do you mean you receive a syntax error stating `Operator '^' cannot be applied to operands of type 'decimal' and 'int'` ? – Sam Axe Mar 22 '15 at 04:33
  • Is that 5% annual interest? If do then the monthly rate is 5/1200, not 5/100. – phoog Mar 22 '15 at 04:38
  • For the vast majority of inputs, `Math.pow()` cannot produce a precise value as its result, so it doesn't make sense to use it with `decimal`. Cast the inputs to `double`. – JLRishe Mar 22 '15 at 04:56
  • @JLRishe the real reason it makes no sense to use Pow with decimal is that there is no overload that accepts decimal arguments. – phoog Mar 22 '15 at 04:59

2 Answers2

4

You should use double for this calculation.

The reason you got incorrect results was that you forgot to divide the annual interest rate by 12 to get the monthly interest rate.

Decimal does not support exponentiation. Also, the caret operator (^) is not for exponentiation in C#; there is no exponentiation operator. You just have to call Math.Pow.

See also https://stackoverflow.com/a/6426826/385844

Community
  • 1
  • 1
phoog
  • 42,068
  • 6
  • 79
  • 117
  • Wow if you never wouldve pointed that out i prob wouldve thrown my laptop out the window eventually. I was trying EVERYTHING and completely forgot to divide by 12. I thought i was coding wrong, well i was, i was just messing up my math lol – SprJD7903 Mar 22 '15 at 18:27
0

double should be plenty accurate for the precisions you are working with. Try changing all your decimals to doubles.

Another problem is your rate. Real rate is entered value/100, which means 1 which you said meant 100% actually ends up as 1%.

Sam Axe
  • 33,313
  • 9
  • 55
  • 89
Tim
  • 2,878
  • 1
  • 14
  • 19