等額本息房貸月供計算

等額本息,是指一種貸款的還款方式。等額本息是在還款期內,每月償還同等數額的貸款(包括本金和利息)。

計算公式

每月還款額=[貸款本金×月利率×(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 秒)

 

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