# 蚂蚁借呗每月等额计算方式

4 篇文章 0 订阅

java

 public static void main(String[] args){
mayiLend(new BigDecimal(Double.toString(10000)),new BigDecimal(Double.toString(0.00035)),new BigDecimal(Double.toString(3)));
}

static void mayiLend(final BigDecimal total, BigDecimal rate, BigDecimal period){//total总额  rate日利率   period期数
Calendar[] dates=new Calendar[period.intValue()];
Calendar now=Calendar.getInstance();
setTimeToMidnight(now);
int now_year = now.get(Calendar.YEAR);
int now_month = now.get(Calendar.MONTH);
int now_day = now.get(Calendar.DAY_OF_MONTH);
int year=now_year;
int month=now_month;
int day=10;//每月10号
Repayment repays[]=new Repayment[period.intValue()];//保存每期还款信息
for(int i=0;i<period.intValue();i++){
repays[i]=new Repayment();
if(++month>11){
month=0;
year++;
}
Calendar c=Calendar.getInstance();
setTimeToMidnight(c);
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
c.set(Calendar.DAY_OF_MONTH, day);
dates[i]=c;
}
BigDecimal average=total.divide(period,2, BigDecimal.ROUND_DOWN);//平均每月还款，最开始设为贷款总额/期数
BigDecimal remain=null;//剩余未还的
BigDecimal step=new BigDecimal(Double.toString(0.01));
do {
if(remain!=null) {
average = average.add(remain.divide(period, 2, BigDecimal.ROUND_DOWN));//平均还款=平均还款(上次)+(剩余(上次)/期数)  保留两位小数点
}
remain = total;
long lastTime = now.getTimeInMillis();
for (int i = 0; i < period.intValue(); i++) {
long rePlayTime = dates[i].getTimeInMillis();//还款时间
BigDecimal days = new BigDecimal(Double.toString(millisecondsToDays(rePlayTime - lastTime)));//计算与上一次还款日间隔(天数)
BigDecimal interest =days.multiply(rate).multiply(remain).setScale(2,BigDecimal.ROUND_DOWN);//计算当期利息，天数*日利率*剩余未还金额
BigDecimal principal = average.subtract(interest);
repays[i].date=dates[i];
repays[i].interest =interest;
repays[i].principal =principal;
remain=remain.subtract(principal);
if(i==period.intValue()-1){//最后一次还款加上(或减)余数
}
lastTime = rePlayTime;
}
}while(remain.abs().compareTo(step.multiply(period))>=0);//如果剩余未还的绝对值大于等于(期数*0.01)，则继续分割
for(int i=0;i<period.intValue();i++){
Repayment repay=repays[i];
System.out.println("第"+(i+1)+"期  时间："+repay.date.get(Calendar.YEAR)+"年"+(repay.date.get(Calendar.MONTH)+1)+"月"
+repay.date.get(Calendar.DAY_OF_MONTH)+"日 还款:"+repay.payment+" = 本金:"+repays[i].principal +" + 利息:"+repays[i].interest);
}
}

static class Repayment {
Calendar date;
BigDecimal interest;
BigDecimal principal;
BigDecimal payment;
}

static void setTimeToMidnight(Calendar calendar) {//设置每天12点0分0秒为分割点
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
}

static int millisecondsToDays(long intervalMs) {//计算天数
return (int) (intervalMs / (1000 * 86400));
}在这里插入代码片


mayiLend(new BigDecimal(Double.toString(10000)),new BigDecimal(Double.toString(0.00035)),new BigDecimal(Double.toString(3)));


mayiLend(new BigDecimal(Double.toString(10008)),new BigDecimal(Double.toString(0.00035)),new BigDecimal(Double.toString(3)));


• 6
点赞
• 7
收藏
觉得还不错? 一键收藏
• 1
评论
02-17 5812
05-19 2375
01-10
01-10
02-23
08-03 526
08-08 107
08-04 644
08-04 234
08-05 312
08-05 774

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