买房子了,按揭还款,为了了解两种贷款方式(等额本金方式和等额本息方式)的区别,以及每个月还款的详细信息,自己写了一个程序用来计算。
两种方式还款结果如下(都是贷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;
}