買房子了,按揭還款,爲了瞭解兩種貸款方式(等額本金方式和等額本息方式)的區別,以及每個月還款的詳細信息,自己寫了一個程序用來計算。
兩種方式還款結果如下(都是貸40W,20年,年利率假定爲0.0655):
等額本金方式和等額本息方式的概念如下:
等額本金:本金保持相同,利息逐月遞減,月還款數遞減。
等額本息:本金逐月遞增,利息逐月遞減,月還款數不變。
理解了概念,要計算就很簡單了。可參見:http://cs.jiwu.com/zhuanti/101905/解釋一下結果中每一列:
年 | 還款的第幾年 |
月 | 還款的第幾年的第幾個月 |
當月本金 | 當前月應還的本金 |
當月利息 | 當前月應還有利息 |
當月還款 | 當前月應還的本金和利息總和 |
共還本金 | 到當前月一共還的本金 |
共還利息 | 到當前月一共還的利息 |
共還款 | 到當前月一共還的本金和利息總和 |
剩餘未還本金 | 剩餘未還的本金 |
提前還總金額 | 如果提前還剩餘的本金,此列表示需要還的本金和日利息總和(按最多30天來算的日利息) |
提前還利息 | 如果提前還剩餘的本金,此列表示需要還的剩餘本金的日利息(按最多30天來算的日利息) |
提前總共還 | 如果提前還剩餘的本金,此列表示從開始還到還完一共所還的總和 |
// Mortgage.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <iomanip>
#include <math.h>
#include <Windows.h>
using namespace std;
double g_dTotalPrincipal = 0.0; // 總共貸款金額
double g_nYears = 0; // 貸款年限
double g_dYearRate = 0.0; // 年利率
// 顯示等額本金還款法
void ShowConstantPrincipal()
{
int nMonths = 12 * g_nYears; // 總還貸月數
double dMonthRate = g_dYearRate / 12; // 月利率
double dMonthPrincipal = g_dTotalPrincipal / nMonths; // 每個月等額本金
double dRestPrincipal = g_dTotalPrincipal; // 剩餘未還本金
double dPrincipalTotalPayBack = 0.0; // 總共還本金
double dInterestTotal = 0.0; // 總共還利息
double dMoneyTotalPayBack = 0.0; // 總共還本息
cout << setw(2) <<"年" << "|" << setw(2) << "月" << "|"
<< setw(10) << "當月本金" << "|" << setw(10) << "當月利息" << "|"
<< setw(10) << "當月還款" << "|" << setw(12) << "共還本金" << "|"
<< setw(12) << "共還利息" << "|" << setw(12) << "共還款" << "|"
<< setw(12) << "剩餘未還本金" << "|"
<< setw(12) << "提前還總金額" << "|" << setw(12) << "提前還利息" << "|"
<< setw(12) << "提前總共還" << "|" << endl;
cout << fixed << setprecision(3);
for (int i = 1; i <= nMonths; i++)
{
// 第幾年
if (1 == i % 12)
{
cout << setw(2) << (i / 12) + 1 << "|";
}
else
{
cout << setw(2) << " " << "|";
}
// 第幾月
cout << setw(2) << (i - 1) % 12 + 1 << "|";
// 當月本金
cout << setw(10) << dMonthPrincipal << "|";
dPrincipalTotalPayBack += dMonthPrincipal;
// 當月利息
cout << setw(10) << dRestPrincipal * dMonthRate << "|";
dInterestTotal += dRestPrincipal * dMonthRate;
// 當月還款
cout << setw(10) << dMonthPrincipal + dRestPrincipal * dMonthRate << "|";
// 共還本金
cout << setw(12) << dPrincipalTotalPayBack << "|";
// 共還利息
cout << setw(12) << dInterestTotal << "|";
// 共還款
cout << setw(12) << dPrincipalTotalPayBack + dInterestTotal << "|";
// 剩餘未還本金
dRestPrincipal -= dMonthPrincipal;
if (dRestPrincipal < 0.0001)
{
// 由於計算誤差,最後一次可能剩餘未還本金爲極小的數
dRestPrincipal = 0.0;
}
cout << setw(12) << dRestPrincipal << "|";
// 提前還總金額
// 以30天來計算
cout << setw(12) << dRestPrincipal + dRestPrincipal * (g_dYearRate / 360) * 30
<< "|";
// 提前多還
cout << setw(12) << dRestPrincipal * (g_dYearRate / 360) * 30 << "|";
// 提前總共還
if (dRestPrincipal > 0.0)
{
// 如果都還完了,就不存在提前還了
cout << setw(12) << dPrincipalTotalPayBack + dInterestTotal + dRestPrincipal
+ dRestPrincipal * (g_dYearRate / 360) * 30 << "|";
}
else
{
cout << setw(12) << 0.0 << "|";
}
cout << endl;
}
}
// 顯示等額本息還款法
void ShowConstantPrincipalInterest()
{
int nMonths = 12 * g_nYears; // 總還貸月數
double dMonthRate = g_dYearRate / 12; // 月利率
double dTotalMoney1Month =
(g_dTotalPrincipal * dMonthRate * pow((1 + dMonthRate), nMonths))
/ (pow((1 + dMonthRate), nMonths) - 1);
double dRestPrincipal = g_dTotalPrincipal; // 剩餘未還本金
double dPrincipalTotalPayBack = 0.0; // 總共還本金
double dInterestTotal = 0.0; // 總共還利息
double dMoneyTotalPayBack = 0.0; // 總共還本息
cout << setw(2) <<"年" << "|" << setw(2) << "月" << "|"
<< setw(10) << "當月本金" << "|" << setw(10) << "當月利息" << "|"
<< setw(10) << "當月還款" << "|" << setw(12) << "共還本金" << "|"
<< setw(12) << "共還利息" << "|" << setw(12) << "共還款" << "|"
<< setw(12) << "剩餘未還本金" << "|"
<< setw(12) << "提前還總金額" << "|" << setw(12) << "提前還利息" << "|"
<< setw(12) << "提前總共還" << "|" << endl;
cout << fixed << setprecision(3);
for (int i = 1; i <= nMonths; i++)
{
// 第幾年
if (1 == i % 12)
{
cout << setw(2) << (i / 12) + 1 << "|";
}
else
{
cout << setw(2) << " " << "|";
}
// 第幾月
cout << setw(2) << (i - 1) % 12 + 1 << "|";
// 當月本金
cout << setw(10) << dTotalMoney1Month - dRestPrincipal * dMonthRate << "|";
dPrincipalTotalPayBack += dTotalMoney1Month - dRestPrincipal * dMonthRate;
// 當月利息
cout << setw(10) << dRestPrincipal * dMonthRate << "|";
dInterestTotal += dRestPrincipal * dMonthRate;
// 當月還款
cout << setw(10) << dTotalMoney1Month << "|";
// 共還本金
cout << setw(12) << dPrincipalTotalPayBack << "|";
// 共還利息
cout << setw(12) << dInterestTotal << "|";
// 共還款
cout << setw(12) << dPrincipalTotalPayBack + dInterestTotal << "|";
// 剩餘未還本金
dRestPrincipal -= dTotalMoney1Month - dRestPrincipal * dMonthRate;
if (dRestPrincipal < 0.0001)
{
// 由於計算誤差,最後一次可能剩餘未還本金爲極小的數
dRestPrincipal = 0.0;
}
cout << setw(12) << dRestPrincipal << "|";
// 提前還總金額
// 以30天來計算
cout << setw(12) << dRestPrincipal + dRestPrincipal * (g_dYearRate / 360) * 30
<< "|";
// 提前多還
cout << setw(12) << dRestPrincipal * (g_dYearRate / 360) * 30 << "|";
// 提前總共還
if (dRestPrincipal > 0.0)
{
// 如果都還完了,就不存在提前還了
cout << setw(12) << dPrincipalTotalPayBack + dInterestTotal + dRestPrincipal
+ dRestPrincipal * (g_dYearRate / 360) * 30 << "|";
}
else
{
cout << setw(12) << 0.0 << "|";
}
cout << endl;
}
}
int main(int argc, char* argv[])
{
const int Console_Screen_Buffer_Min_Width = 140;
const int Console_Screen_Buffer_Min_Heigth = 650;
CONSOLE_SCREEN_BUFFER_INFO conScrBufInfo;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &conScrBufInfo);
int nConSrcBufWidth = max(Console_Screen_Buffer_Min_Width, conScrBufInfo.dwSize.X);
int nConSrcBufHeigth = max(Console_Screen_Buffer_Min_Heigth, conScrBufInfo.dwSize.Y);
COORD coordDest = {nConSrcBufWidth, nConSrcBufHeigth};
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coordDest);
conScrBufInfo.srWindow.Right = nConSrcBufWidth - 1;
SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE), TRUE, &conScrBufInfo.srWindow);
cout << "輸入還貸總額:";
cin >> g_dTotalPrincipal;
cout << "輸入還貸總年數:";
cin >> g_nYears;
cout << "輸入年利率:";
cin >> g_dYearRate;
int nMortgateType = 0;
cout << "輸入還貸方式(0:等額本金方式;1:等額本息方式):";
cin >> nMortgateType;
switch(nMortgateType)
{
case 0: // 等額本金方式
ShowConstantPrincipal();
break;
case 1: // 等額本息方式
ShowConstantPrincipalInterest();
break;
default:
break;
}
cout << "按任意鍵退出...";
getch();
return 0;
}