等额本息房贷月供计算

等额本息,是指一种贷款的还款方式。等额本息是在还款期内,每月偿还同等数额的贷款(包括本金和利息)。

计算公式

每月还款额=[贷款本金×月利率×(1+月利率)^还款月数]÷[(1+月利率)^还款月数-1]

还款公式推导

设贷款总额为M,银行月利率为p,总期数为n(个月),月还款额为x,则各个月所欠银行贷款为:

a[n] =(1 + p)*a[n - 1] -  x ;其中a[0]=M

由于还款总期数为n,也即第n月刚好还完银行所有贷款,令a[n]=0,求x:

JAVA实现

import java.util.Scanner;

public class ComputeLoan {
  public static void main(String[] args) {   
    // Create a Scanner
    Scanner input = new Scanner(System.in);

    // Enter yearly interest rate
    System.out.print("Enter yearly interest rate, for example 8.25: ");
    double annualInterestRate = input.nextDouble();
    
    // Obtain monthly interest rate
    double monthlyInterestRate = annualInterestRate / 1200;

    // Enter number of years
    System.out.print(
      "Enter number of years as an integer, for example 5: ");
    int numberOfYears = input.nextInt();
    
    // Enter loan amount
    System.out.print("Enter loan amount, for example 120000.95: ");
    double loanAmount = input.nextDouble();
    
    // Calculate payment
    double monthlyPayment = loanAmount * monthlyInterestRate / (1
      - 1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12));
    double totalPayment = monthlyPayment * numberOfYears * 12;

    // Display results
    System.out.println("The monthly payment is $" + 
      (int)(monthlyPayment * 100) / 100.0);
    System.out.println("The total payment is $" + 
      (int)(totalPayment * 100) / 100.0);
  }
}
run:
Enter yearly interest rate, for example 8.25: 5.5
Enter number of years as an integer, for example 5: 20
Enter loan amount, for example 120000.95: 500000
The monthly payment is $3439.43
The total payment is $825464.76
成功构建 (总时间: 49 秒)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章