# java实现贷款计算器（工具类）

20 篇文章 0 订阅
1 篇文章 0 订阅
1 篇文章 0 订阅

###### 主要参数：
	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;
}


• 3
点赞
• 17
收藏
• 打赏
• 0
评论
05-02 4975
05-06
10-11 43
07-31 520
11-24 4281
07-30 371
12-15
03-28 2486
04-03 1212
10-14
02-07

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

iboylee

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。