简体   繁体   中英

Simple C# mortgage formula calculating wrong

Here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MortgageApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = "", year, principle, month;
            double r, y, p;
            bool valid = false; 
            y = 0;
            r = 0;
            p = 0;



            while (valid == false)
            {

                Console.WriteLine("Enter the duration of the loan (Number of Years): ");
                input = Console.ReadLine();

                if (double.TryParse(input, out y))
                {

                    Console.WriteLine(y);
                    valid = true;

                }
            }

            valid = false;
            while (valid == false)
            {
                Console.WriteLine("Enter the princple ammount: ");
                input = Console.ReadLine();

                if (double.TryParse(input, out p))
                {
                    Console.WriteLine(p);
                    valid = true;
                }

            }


            valid = false;
            while (valid == false)
            {
                Console.WriteLine("Enter the Interest Rate ");
                input = Console.ReadLine();

                if (double.TryParse(input, out r))
                {

                    valid = true;


                }

            }

            r = r / 100;

            Console.WriteLine(r);
            double top = p * r / 1200;
            Console.WriteLine(top);
            double x = (1 + (r / 1200.0));
            Console.WriteLine(x);
            double n = -12 * y;
            Console.WriteLine(n);
            double buttom = (1 - (Math.Pow(x, n)) );
            Console.WriteLine(buttom);
            double solution = top / buttom;

            Console.WriteLine(solution);
            Console.ReadLine();


        }
    }
}

This is suppose to be a simple mortgage app. I have the functionality but the formula is not correct.

Not sure if it's because I'm using doubles or if the problem is with my coding.

(pr / 1200.0) / (1 - (1.0 + r / 1200.0) ^(-12.0 n)),

Where

  • p = principal (dollars)
  • n = number of years
  • r = interest rate (percent)
  • m = monthly payment

So I think the direct answer is that you're dividing the rate by 100, then dividing it again by 1200. You should either not divide by 100 to start with, or only divide by 12 later (I like the second option because it makes it clear you're talking about 12 months).

Another thing you might consider, in order to reduce repeated code, is to factor out a new function that gets a double from the user. Something like:

private static double GetDoubleFromUser(string prompt)
{
    double result;

    while (true)
    {
        if (prompt != null) Console.Write(prompt);
        var input = Console.ReadLine();
        if (double.TryParse(input, out result)) break;
        Console.WriteLine("Sorry, that is not a valid number. Please try again...");
    }

    return result;
}

Now, when you need a double, you just call this function and pass the prompt string. This makes your code much cleaner and easier to read. For example, your code could now be written as:

private static void Main()
{
    double years = GetDoubleFromUser("Enter the duration of the loan (in years): ");
    double principal = GetDoubleFromUser("Enter the princple ammount: ");
    double rate = GetDoubleFromUser("Enter the interest rate: ") / 100;

    Console.WriteLine("\nBased on these values entered:");
    Console.WriteLine(" - Number of years .... {0}", years);
    Console.WriteLine(" - Principal amount ... {0:c}", principal);
    Console.WriteLine(" - Interest rate ...... {0:p}", rate);

    double monthlyRate = rate / 12;
    double payments = 12 * years;

    double result =
        principal *
        (monthlyRate * Math.Pow(1 + monthlyRate, payments)) /
        (Math.Pow(1 + monthlyRate, payments) - 1);

    Console.WriteLine("\nYour monthly payment will be: {0:c}", result);
    Console.ReadLine();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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