简单的C#抵押公式计算错误

Simple C# mortgage formula calculating wrong

这是我的代码:

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


        }
    }
}

这应该是一个简单的抵押应用程序。我有功能,但公式不正确。

不确定是因为我使用的是双打还是我的编码有问题。

(p r / 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

所以我认为直接的答案是您将比率除以 100,然后再除以 1200。您要么一开始就不要除以 100,要么稍后再除以 12(我喜欢第二个选项,因为它清楚地表明您在谈论 12 个月)。

为了减少重复代码,您可能会考虑的另一件事是分解出一个从用户那里获得双精度值的新函数。类似于:

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

现在,当您需要双精度值时,只需调用此函数并传递提示字符串即可。这使您的代码更加清晰易读。例如,您的代码现在可以写成:

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