java實現貸款計算器(工具類)

貸款相關內容請參考 等額本金與等額本息

等額本息:每個月的還款金額相同

等額本息法最重要的一個特點是每月的還款額相同,從本質上來說是本金所佔比例逐月遞增,利息所佔比例逐月遞減,月還款數不變,即在月供“本金與利息”的分配比例中,前半段時期所還的利息比例大、本金比例小,還款期限過半後逐步轉爲本金比例大、利息比例小,其計算公式爲:

每月還本付息金額 =[ 本金 x 月利率 x(1+月利率)貸款月數 ] / [(1+月利率)還款月數 - 1]

每月利息 = 剩餘本金x貸款月利率

還款總利息=貸款額貸款月數月利率*(1+月利率)貸款月數/【(1+月利率)還款月數 - 1】-貸款額

還款總額=還款月數貸款額月利率*(1+月利率)貸款月數/【(1+月利率)還款月數 - 1】

等額本金:每個月還本金相同,但每月還款金額不同

等額本金法最大的特點是每月的還款額不同,呈現逐月遞減的狀態;它是將貸款本金按還款的總月數均分,再加上上期剩餘本金的利息,這樣就形成月還款額,所以等額本金法第一個月的還款額最多 ,然後逐月減少,越還越少,計算公式爲:

每月還本付息金額=(本金/還款月數)+(本金-累計已還本金)×月利率

每月本金=總本金/還款月數

每月利息=(本金-累計已還本金)×月利率

還款總利息=(還款月數+1)貸款額月利率/2

還款總額=(還款月數+1)貸款額月利率/2+貸款額

注意:在等額本金法中,人們每月歸還的本金額始終不變,利息隨剩餘本金的減少而減少,因而其每月還款額逐漸減少。

主要參數:
	private double loanMoney;  //貸款金額 
	
	private int years; //貸款年限
	
	private double yIns; //年利率

	private double totalMoney;  //還款金額(本金+利息)
	
	private double totalInterests; //利息總額
	
	private double detail[][];  //每月還款詳情
對外接口
//等額本息
public void EqualPrincipalandInterestMethod() {
		
		double mIns = yIns / 100 / 12; //月利率
		int months = (years * 12); //還款所需月份
		double pow = Math.pow(1 + mIns,months); 
		double remains = loanMoney;
		totalMoney = (months * loanMoney * mIns * pow) / (pow - 1);  //總還款金額
		totalMoney = Math.floor(totalMoney * 100 + 0.5) / 100;  //floor函數 保留兩位小數
		totalInterests = totalMoney - loanMoney;
		totalInterests = Math.floor(totalInterests * 100 + 0.5) / 100;
		double temp[][] = new double[months][3];
		for (int i = 0; i < months; i++) {
			if(i == months - 1) {
				temp[i][1] = remains * mIns; 
				temp[i][1] = Math.floor(temp[i][1] * 100 + 0.5) / 100;
				temp[i][0] = remains; 
				temp[i][0] = Math.floor(temp[i][0] * 100 + 0.5) / 100;
				temp[i][2] = temp[i][0] + temp[i][1];
				temp[i][2] = Math.floor(temp[i][2] * 100 + 0.5) / 100;
				break;
			} 
			//由於精度問題 最後一個月實際的本金會有差別 需要單獨計算
			temp[i][1] = remains * mIns;
			temp[i][1] = Math.floor(temp[i][1] * 100 + 0.5) / 100;
			temp[i][2] = totalMoney / months;
			temp[i][2] = Math.floor(temp[i][2] * 100 + 0.5) / 100;
			temp[i][0] = temp[i][2] - temp[i][1];
			temp[i][0] = Math.floor(temp[i][0] * 100 + 0.5) / 100;
			remains -= temp[i][0];
		}
		//temp[][0]爲每月還款本金 temp[][1]爲每月還款利息 temp[][2]爲每月還款總額
		detail = temp;
	}
	//等額本金
	public void EqualPrincipalMethod() {
		
		double mIns = yIns / 100 / 12; //月利率
		int months = (years * 12);
		double remains = loanMoney;
		double sum = 0; // 總計還款金額
		double temp[][] = new double[months][3];
		for (int i = 0; i < months; i++)
		{
			temp[i][0] = loanMoney / months;
			temp[i][0] = Math.floor(temp[i][0] * 100 + 0.5) / 100;
			temp[i][1] = remains * mIns;
			temp[i][1] = Math.floor(temp[i][1] * 100 + 0.5) / 100;
			remains -= temp[i][0];
			temp[i][2] = temp[i][0] + temp[i][1];
			temp[i][2] = Math.floor(temp[i][2] * 100 + 0.5) / 100;
			sum += temp[i][2];
		}
		//temp[][0]爲每月還款本金 temp[][1]爲每月還款利息 temp[][2]爲每月還款總額
		detail = temp;
		totalMoney = sum;
		totalMoney = Math.floor(totalMoney * 100 + 0.5) / 100;
		totalInterests = totalMoney - loanMoney;
		totalInterests = Math.floor(totalInterests * 100 + 0.5) / 100;
	}

前端頁面顯示(layUI)
在這裏插入圖片描述
在這裏插入圖片描述

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