This commit is contained in:
muzi1 2023-01-16 12:09:58 +08:00
parent a26fc725c0
commit 49d1704b65
5 changed files with 394 additions and 114 deletions

View File

@ -7,6 +7,8 @@ import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.potenza.domain.TbBorrower; import com.ruoyi.potenza.domain.TbBorrower;
import com.ruoyi.potenza.domain.vo.IdVo; import com.ruoyi.potenza.domain.vo.IdVo;
import com.ruoyi.potenza.service.TbBorrowerService; import com.ruoyi.potenza.service.TbBorrowerService;
import com.ruoyi.potenza.utils.AverageCapitalPlusInterestUtils;
import com.ruoyi.potenza.utils.AverageCapitalUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import java.util.List; import java.util.List;
@ -124,6 +126,12 @@ public class BorrowerController extends BaseController {
return tbBorrowerService.loans(tbBorrower); return tbBorrowerService.loans(tbBorrower);
} }
@PostMapping("ttt")
public void ttt(){
AverageCapitalUtils.main();
AverageCapitalPlusInterestUtils.main();
}

View File

@ -12,7 +12,9 @@ import com.ruoyi.potenza.utils.AverageCapitalUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* @author 86155 * @author 86155
@ -121,11 +123,13 @@ public class TbBorrowerPeriodsServiceImpl extends ServiceImpl<TbBorrowerPeriodsM
return AjaxResult.success(principalInterestCount); return AjaxResult.success(principalInterestCount);
} }
//等额本金 //等额本金
double principalInterestCount = AverageCapitalUtils. // double principalInterestCount = AverageCapitalUtils.
getPrincipalInterestCount(periodDetailVo.getBorrowerMoney(), // getPrincipalInterestCount(periodDetailVo.getBorrowerMoney(),
tbBorrowerPeriods.getRateInterest()/100, // tbBorrowerPeriods.getRateInterest()/100,
tbBorrowerPeriods.getPeriodsName()); // tbBorrowerPeriods.getPeriodsName());
return AjaxResult.success(principalInterestCount); // return AjaxResult.success(principalInterestCount);
return AjaxResult.error();
} }
} }

View File

@ -115,10 +115,10 @@ public class TbBorrowerServiceImpl extends ServiceImpl<TbBorrowerMapper, TbBorro
//查询利率 //查询利率
TbBorrowerPeriods tbBorrowerPeriods = tbBorrowerPeriodsMapper.selectTbBorrowerPeriodsByPeriodsId(periodsid); TbBorrowerPeriods tbBorrowerPeriods = tbBorrowerPeriodsMapper.selectTbBorrowerPeriodsByPeriodsId(periodsid);
double perMonthPrincipalInterest = AverageCapitalPlusInterestUtils. // double perMonthPrincipalInterest = AverageCapitalPlusInterestUtils.
getPerMonthPrincipalInterest(tbBorrower.getBorrowerMoney(), // getPerMonthPrincipalInterest(tbBorrower.getBorrowerMoney(),
tbBorrowerPeriods.getRateInterest()/100, // tbBorrowerPeriods.getRateInterest()/100,
tbBorrowerPeriods.getPeriodsName()); // tbBorrowerPeriods.getPeriodsName());
double principalInterestCount = AverageCapitalPlusInterestUtils. double principalInterestCount = AverageCapitalPlusInterestUtils.

View File

@ -17,32 +17,160 @@ import java.util.Map;
* @version 1.0 * @version 1.0
*/ */
public class AverageCapitalPlusInterestUtils { public class AverageCapitalPlusInterestUtils {
// /**
// * 等额本息计算获取还款方式为等额本息的每月偿还本金和利息
// * <p>
// * 公式每月偿还本息=贷款本金×月利率×(1月利率)还款月数÷(1月利率)还款月数-1
// *
// * @param invest 总借款额贷款本金
// * @param yearRate 年利率
// * @param month 还款总月数
// * @return 每月偿还本金和利息, 不四舍五入直接截取小数点最后两位
// */
// public static double getPerMonthPrincipalInterest(double invest, double yearRate, int totalmonth) {
// double monthRate = yearRate / 12;
// BigDecimal monthIncome = new BigDecimal(invest)
// .multiply(new BigDecimal(monthRate * Math.pow(1 + monthRate, totalmonth)))
// .divide(new BigDecimal(Math.pow(1 + monthRate, totalmonth) - 1), 2, BigDecimal.ROUND_DOWN);
// return monthIncome.doubleValue();
// }
//
// /**
// * 等额本息计算获取还款方式为等额本息的每月偿还利息
// * <p>
// * 公式每月偿还利息=贷款本金×月利率×(1+月利率)^还款月数-(1+月利率)^(还款月序号-1)÷(1+月利率)^还款月数-1
// *
// * @param invest 总借款额贷款本金
// * @param yearRate 年利率
// * @param month 还款总月数
// * @return 每月偿还利息
// */
// public static Map<Integer, BigDecimal> getPerMonthInterest(double invest, double yearRate, int totalmonth) {
// Map<Integer, BigDecimal> map = new HashMap<Integer, BigDecimal>();
// double monthRate = yearRate / 12;
// BigDecimal monthInterest;
// for (int i = 1; i < totalmonth + 1; i++) {
// BigDecimal multiply = new BigDecimal(invest).multiply(new BigDecimal(monthRate));
// BigDecimal sub = new BigDecimal(Math.pow(1 + monthRate, totalmonth)).subtract(new BigDecimal(Math.pow(1 + monthRate, i - 1)));
// monthInterest = multiply.multiply(sub).divide(new BigDecimal(Math.pow(1 + monthRate, totalmonth) - 1), 6, BigDecimal.ROUND_DOWN);
// monthInterest = monthInterest.setScale(2, BigDecimal.ROUND_DOWN);
// map.put(i, monthInterest);
// }
// return map;
// }
//
// /**
// * 等额本息计算获取还款方式为等额本息的每月偿还本金
// *
// * @param invest 总借款额贷款本金
// * @param yearRate 年利率
// * @param month 还款总月数
// * @return 每月偿还本金
// */
// public static Map<Integer, BigDecimal> getPerMonthPrincipal(double invest, double yearRate, int totalmonth) {
// double monthRate = yearRate / 12;
// BigDecimal monthIncome = new BigDecimal(invest)
// .multiply(new BigDecimal(monthRate * Math.pow(1 + monthRate, totalmonth)))
// .divide(new BigDecimal(Math.pow(1 + monthRate, totalmonth) - 1), 2, BigDecimal.ROUND_DOWN);
// Map<Integer, BigDecimal> mapInterest = getPerMonthInterest(invest, yearRate, totalmonth);
// Map<Integer, BigDecimal> mapPrincipal = new HashMap<Integer, BigDecimal>();
//
// for (Map.Entry<Integer, BigDecimal> entry : mapInterest.entrySet()) {
// mapPrincipal.put(entry.getKey(), monthIncome.subtract(entry.getValue()));
// }
// return mapPrincipal;
// }
//
// /**
// * 等额本息计算获取还款方式为等额本息的总利息
// *
// * @param invest 总借款额贷款本金
// * @param yearRate 年利率
// * @param month 还款总月数
// * @return 总利息
// */
// public static double getInterestCount(double invest, double yearRate, int totalmonth) {
// BigDecimal count = new BigDecimal(0);
// Map<Integer, BigDecimal> mapInterest = getPerMonthInterest(invest, yearRate, totalmonth);
//
// for (Map.Entry<Integer, BigDecimal> entry : mapInterest.entrySet()) {
// count = count.add(entry.getValue());
// }
// return count.doubleValue();
// }
//
// /**
// * 应还本金总和
// *
// * @param invest 总借款额贷款本金
// * @param yearRate 年利率
// * @param month 还款总月数
// * @return 应还本金总和
// */
// public static double getPrincipalInterestCount(double invest, double yearRate, int totalmonth) {
// double monthRate = yearRate / 12;
// BigDecimal perMonthInterest = new BigDecimal(invest)
// .multiply(new BigDecimal(monthRate * Math.pow(1 + monthRate, totalmonth)))
// .divide(new BigDecimal(Math.pow(1 + monthRate, totalmonth) - 1), 2, BigDecimal.ROUND_DOWN);
// BigDecimal count = perMonthInterest.multiply(new BigDecimal(totalmonth));
// count = count.setScale(2, BigDecimal.ROUND_DOWN);
// return count.doubleValue();
// }
//
//
// /**
// * @param args
// */
// public static void main(String[] args) {
// double invest = 20000; // 本金
// int month = 12;
// double yearRate = 0.15; // 年利率
// double perMonthPrincipalInterest = getPerMonthPrincipalInterest(invest, yearRate, month);
// System.out.println("等额本息---每月还款本息:" + perMonthPrincipalInterest);
// Map<Integer, BigDecimal> mapInterest = getPerMonthInterest(invest, yearRate, month);
// System.out.println("等额本息---每月还款利息:" + mapInterest);
// Map<Integer, BigDecimal> mapPrincipal = getPerMonthPrincipal(invest, yearRate, month);
// System.out.println("等额本息---每月还款本金:" + mapPrincipal);
// double count = getInterestCount(invest, yearRate, month);
// System.out.println("等额本息---总利息:" + count);
// double principalInterestCount = getPrincipalInterestCount(invest, yearRate, month);
// System.out.println("等额本息---应还本息总和:" + principalInterestCount);
//
// }
/** /**
* 等额本息计算获取还款方式为等额本息的每月偿还本金和利息 * 等额本息计算获取还款方式为等额本息的每月偿还本金和利息
* <p> *
* 公式每月偿还本息=贷款本金×月利率×(1月利率)还款月数÷(1月利率)还款月数-1 * 公式每月偿还本息=贷款本金×月利率×(1月利率)还款月数÷(1月利率)还款月数-1
* *
* @param invest 总借款额贷款本金 * @param invest
* @param yearRate 年利率 * 总借款额贷款本金
* @param month 还款总月数 * @param yearRate
* @return 每月偿还本金和利息, 不四舍五入直接截取小数点最后两位 * 年利率
* @param month
* 还款总月数
* @return 每月偿还本金和利息,不四舍五入直接截取小数点最后两位
*/ */
public static double getPerMonthPrincipalInterest(double invest, double yearRate, int totalmonth) { public static BigDecimal getPerMonthPrincipalInterest(double invest, double yearRate, int totalmonth) {
double monthRate = yearRate / 12; double monthRate = yearRate / 12;
BigDecimal monthIncome = new BigDecimal(invest) BigDecimal monthIncome = new BigDecimal(invest)
.multiply(new BigDecimal(monthRate * Math.pow(1 + monthRate, totalmonth))) .multiply(new BigDecimal(monthRate * Math.pow(1 + monthRate, totalmonth)))
.divide(new BigDecimal(Math.pow(1 + monthRate, totalmonth) - 1), 2, BigDecimal.ROUND_DOWN); .divide(new BigDecimal(Math.pow(1 + monthRate, totalmonth) - 1), 2, BigDecimal.ROUND_UP);
return monthIncome.doubleValue(); //return monthIncome.doubleValue();
return monthIncome;
} }
/** /**
* 等额本息计算获取还款方式为等额本息的每月偿还利息 * 等额本息计算获取还款方式为等额本息的每月偿还利息
* <p> *
* 公式每月偿还利息=贷款本金×月利率×(1+月利率)^还款月数-(1+月利率)^(还款月序号-1)÷(1+月利率)^还款月数-1 * 公式每月偿还利息=贷款本金×月利率×(1+月利率)^还款月数-(1+月利率)^(还款月序号-1)÷(1+月利率)^还款月数-1
* *
* @param invest 总借款额贷款本金 * @param invest
* @param yearRate 年利率 * 总借款额贷款本金
* @param month 还款总月数 * @param yearRate
* 年利率
* @param month
* 还款总月数
* @return 每月偿还利息 * @return 每月偿还利息
*/ */
public static Map<Integer, BigDecimal> getPerMonthInterest(double invest, double yearRate, int totalmonth) { public static Map<Integer, BigDecimal> getPerMonthInterest(double invest, double yearRate, int totalmonth) {
@ -51,8 +179,10 @@ public class AverageCapitalPlusInterestUtils {
BigDecimal monthInterest; BigDecimal monthInterest;
for (int i = 1; i < totalmonth + 1; i++) { for (int i = 1; i < totalmonth + 1; i++) {
BigDecimal multiply = new BigDecimal(invest).multiply(new BigDecimal(monthRate)); BigDecimal multiply = new BigDecimal(invest).multiply(new BigDecimal(monthRate));
BigDecimal sub = new BigDecimal(Math.pow(1 + monthRate, totalmonth)).subtract(new BigDecimal(Math.pow(1 + monthRate, i - 1))); BigDecimal sub = new BigDecimal(Math.pow(1 + monthRate, totalmonth))
monthInterest = multiply.multiply(sub).divide(new BigDecimal(Math.pow(1 + monthRate, totalmonth) - 1), 6, BigDecimal.ROUND_DOWN); .subtract(new BigDecimal(Math.pow(1 + monthRate, i - 1)));
monthInterest = multiply.multiply(sub).divide(new BigDecimal(Math.pow(1 + monthRate, totalmonth) - 1), 2,
BigDecimal.ROUND_DOWN);
monthInterest = monthInterest.setScale(2, BigDecimal.ROUND_DOWN); monthInterest = monthInterest.setScale(2, BigDecimal.ROUND_DOWN);
map.put(i, monthInterest); map.put(i, monthInterest);
} }
@ -62,9 +192,12 @@ public class AverageCapitalPlusInterestUtils {
/** /**
* 等额本息计算获取还款方式为等额本息的每月偿还本金 * 等额本息计算获取还款方式为等额本息的每月偿还本金
* *
* @param invest 总借款额贷款本金 * @param invest
* @param yearRate 年利率 * 总借款额贷款本金
* @param month 还款总月数 * @param yearRate
* 年利率
* @param month
* 还款总月数
* @return 每月偿还本金 * @return 每月偿还本金
*/ */
public static Map<Integer, BigDecimal> getPerMonthPrincipal(double invest, double yearRate, int totalmonth) { public static Map<Integer, BigDecimal> getPerMonthPrincipal(double invest, double yearRate, int totalmonth) {
@ -84,9 +217,12 @@ public class AverageCapitalPlusInterestUtils {
/** /**
* 等额本息计算获取还款方式为等额本息的总利息 * 等额本息计算获取还款方式为等额本息的总利息
* *
* @param invest 总借款额贷款本金 * @param invest
* @param yearRate 年利率 * 总借款额贷款本金
* @param month 还款总月数 * @param yearRate
* 年利率
* @param month
* 还款总月数
* @return 总利息 * @return 总利息
*/ */
public static double getInterestCount(double invest, double yearRate, int totalmonth) { public static double getInterestCount(double invest, double yearRate, int totalmonth) {
@ -102,9 +238,12 @@ public class AverageCapitalPlusInterestUtils {
/** /**
* 应还本金总和 * 应还本金总和
* *
* @param invest 总借款额贷款本金 * @param invest
* @param yearRate 年利率 * 总借款额贷款本金
* @param month 还款总月数 * @param yearRate
* 年利率
* @param month
* 还款总月数
* @return 应还本金总和 * @return 应还本金总和
*/ */
public static double getPrincipalInterestCount(double invest, double yearRate, int totalmonth) { public static double getPrincipalInterestCount(double invest, double yearRate, int totalmonth) {
@ -117,16 +256,21 @@ public class AverageCapitalPlusInterestUtils {
return count.doubleValue(); return count.doubleValue();
} }
/** /**
* @param args * @param args
*/ */
public static void main(String[] args) { public static void main() {
double invest = 20000; // 本金 double invest = 10000; // 本金
int month = 12; int month = 12;
double yearRate = 0.15; // 年利率 double yearRate = 0.015; // 年利率
double perMonthPrincipalInterest = getPerMonthPrincipalInterest(invest, yearRate, month); BigDecimal perMonthPrincipalInterest = getPerMonthPrincipalInterest(invest, yearRate, month);
System.out.println("等额本息---每月还款本息:" + perMonthPrincipalInterest); System.out.println("等额本息---每月还款本息:" + perMonthPrincipalInterest);
System.out.println("等额本息---每月还款本息:" + getPerMonthPrincipalInterest(invest, yearRate, 3));
System.out.println("等额本息---每月还款本息:" + getPerMonthPrincipalInterest(invest, yearRate, 6));
System.out.println("等额本息---每月还款本息:" + getPerMonthPrincipalInterest(invest, yearRate, 9));
System.out.println("等额本息---每月还款本息:" + getPerMonthPrincipalInterest(invest, yearRate, 12));
System.out.println("等额本息---每月还款本息:" + getPerMonthPrincipalInterest(invest, yearRate, 15));
System.out.println("等额本息---每月还款本息:" + getPerMonthPrincipalInterest(invest, yearRate, 18));
Map<Integer, BigDecimal> mapInterest = getPerMonthInterest(invest, yearRate, month); Map<Integer, BigDecimal> mapInterest = getPerMonthInterest(invest, yearRate, month);
System.out.println("等额本息---每月还款利息:" + mapInterest); System.out.println("等额本息---每月还款利息:" + mapInterest);
Map<Integer, BigDecimal> mapPrincipal = getPerMonthPrincipal(invest, yearRate, month); Map<Integer, BigDecimal> mapPrincipal = getPerMonthPrincipal(invest, yearRate, month);
@ -135,7 +279,7 @@ public class AverageCapitalPlusInterestUtils {
System.out.println("等额本息---总利息:" + count); System.out.println("等额本息---总利息:" + count);
double principalInterestCount = getPrincipalInterestCount(invest, yearRate, month); double principalInterestCount = getPrincipalInterestCount(invest, yearRate, month);
System.out.println("等额本息---应还本息总和:" + principalInterestCount); System.out.println("等额本息---应还本息总和:" + principalInterestCount);
} }
} }

View File

@ -10,10 +10,154 @@ import java.util.Map;
* @date 2023/1/15 20:49 * @date 2023/1/15 20:49
*/ */
public class AverageCapitalUtils { public class AverageCapitalUtils {
// /**
// * 等额本息计算获取还款方式为等额本息的每月偿还本金和利息
// *
// * 公式每月偿还本息=贷款本金×月利率×(1月利率)还款月数÷(1月利率)还款月数-1
// *
// * @param invest
// * 总借款额贷款本金
// * @param yearRate
// * 年利率
// * @param month
// * 还款总月数
// * @return 每月偿还本金和利息,不四舍五入直接截取小数点最后两位
// */
// public static BigDecimal getPerMonthPrincipalInterest(double invest, double yearRate, int totalmonth) {
// double monthRate = yearRate / 12;
// BigDecimal monthIncome = new BigDecimal(invest)
// .multiply(new BigDecimal(monthRate * Math.pow(1 + monthRate, totalmonth)))
// .divide(new BigDecimal(Math.pow(1 + monthRate, totalmonth) - 1), 2, BigDecimal.ROUND_UP);
// //return monthIncome.doubleValue();
// return monthIncome;
// }
//
// /**
// * 等额本息计算获取还款方式为等额本息的每月偿还利息
// *
// * 公式每月偿还利息=贷款本金×月利率×(1+月利率)^还款月数-(1+月利率)^(还款月序号-1)÷(1+月利率)^还款月数-1
// *
// * @param invest
// * 总借款额贷款本金
// * @param yearRate
// * 年利率
// * @param month
// * 还款总月数
// * @return 每月偿还利息
// */
// public static Map<Integer, BigDecimal> getPerMonthInterest(double invest, double yearRate, int totalmonth) {
// Map<Integer, BigDecimal> map = new HashMap<Integer, BigDecimal>();
// double monthRate = yearRate / 12;
// BigDecimal monthInterest;
// for (int i = 1; i < totalmonth + 1; i++) {
// BigDecimal multiply = new BigDecimal(invest).multiply(new BigDecimal(monthRate));
// BigDecimal sub = new BigDecimal(Math.pow(1 + monthRate, totalmonth))
// .subtract(new BigDecimal(Math.pow(1 + monthRate, i - 1)));
// monthInterest = multiply.multiply(sub).divide(new BigDecimal(Math.pow(1 + monthRate, totalmonth) - 1), 2,
// BigDecimal.ROUND_DOWN);
// monthInterest = monthInterest.setScale(2, BigDecimal.ROUND_DOWN);
// map.put(i, monthInterest);
// }
// return map;
// }
//
// /**
// * 等额本息计算获取还款方式为等额本息的每月偿还本金
// *
// * @param invest
// * 总借款额贷款本金
// * @param yearRate
// * 年利率
// * @param month
// * 还款总月数
// * @return 每月偿还本金
// */
// public static Map<Integer, BigDecimal> getPerMonthPrincipal(double invest, double yearRate, int totalmonth) {
// double monthRate = yearRate / 12;
// BigDecimal monthIncome = new BigDecimal(invest)
// .multiply(new BigDecimal(monthRate * Math.pow(1 + monthRate, totalmonth)))
// .divide(new BigDecimal(Math.pow(1 + monthRate, totalmonth) - 1), 2, BigDecimal.ROUND_DOWN);
// Map<Integer, BigDecimal> mapInterest = getPerMonthInterest(invest, yearRate, totalmonth);
// Map<Integer, BigDecimal> mapPrincipal = new HashMap<Integer, BigDecimal>();
//
// for (Map.Entry<Integer, BigDecimal> entry : mapInterest.entrySet()) {
// mapPrincipal.put(entry.getKey(), monthIncome.subtract(entry.getValue()));
// }
// return mapPrincipal;
// }
//
// /**
// * 等额本息计算获取还款方式为等额本息的总利息
// *
// * @param invest
// * 总借款额贷款本金
// * @param yearRate
// * 年利率
// * @param month
// * 还款总月数
// * @return 总利息
// */
// public static double getInterestCount(double invest, double yearRate, int totalmonth) {
// BigDecimal count = new BigDecimal(0);
// Map<Integer, BigDecimal> mapInterest = getPerMonthInterest(invest, yearRate, totalmonth);
//
// for (Map.Entry<Integer, BigDecimal> entry : mapInterest.entrySet()) {
// count = count.add(entry.getValue());
// }
// return count.doubleValue();
// }
//
// /**
// * 应还本金总和
// *
// * @param invest
// * 总借款额贷款本金
// * @param yearRate
// * 年利率
// * @param month
// * 还款总月数
// * @return 应还本金总和
// */
// public static double getPrincipalInterestCount(double invest, double yearRate, int totalmonth) {
// double monthRate = yearRate / 12;
// BigDecimal perMonthInterest = new BigDecimal(invest)
// .multiply(new BigDecimal(monthRate * Math.pow(1 + monthRate, totalmonth)))
// .divide(new BigDecimal(Math.pow(1 + monthRate, totalmonth) - 1), 2, BigDecimal.ROUND_DOWN);
// BigDecimal count = perMonthInterest.multiply(new BigDecimal(totalmonth));
// count = count.setScale(2, BigDecimal.ROUND_DOWN);
// return count.doubleValue();
// }
//
// /**
// * @param args
// */
// public static void main(String[] args) {
// double invest = 38988; // 本金
// int month = 12;
// double yearRate = 0.15; // 年利率
// BigDecimal perMonthPrincipalInterest = getPerMonthPrincipalInterest(invest, yearRate, month);
// System.out.println("等额本息---每月还款本息:" + perMonthPrincipalInterest);
// System.out.println("等额本息---每月还款本息:" + getPerMonthPrincipalInterest(invest, yearRate, 3));
// System.out.println("等额本息---每月还款本息:" + getPerMonthPrincipalInterest(invest, yearRate, 6));
// System.out.println("等额本息---每月还款本息:" + getPerMonthPrincipalInterest(invest, yearRate, 9));
// System.out.println("等额本息---每月还款本息:" + getPerMonthPrincipalInterest(invest, yearRate, 12));
// System.out.println("等额本息---每月还款本息:" + getPerMonthPrincipalInterest(invest, yearRate, 15));
// System.out.println("等额本息---每月还款本息:" + getPerMonthPrincipalInterest(invest, yearRate, 18));
// /*Map<Integer, BigDecimal> mapInterest = getPerMonthInterest(invest, yearRate, month);
// System.out.println("等额本息---每月还款利息:" + mapInterest);
// Map<Integer, BigDecimal> mapPrincipal = getPerMonthPrincipal(invest, yearRate, month);
// System.out.println("等额本息---每月还款本金:" + mapPrincipal);
// double count = getInterestCount(invest, yearRate, month);
// System.out.println("等额本息---总利息:" + count);
// double principalInterestCount = getPrincipalInterestCount(invest, yearRate, month);
// System.out.println("等额本息---应还本息总和:" + principalInterestCount);*/
// }
/** /**
* 等额本息计算获取还款方式为等额本息的每月偿还本金和利息 * 等额本金计算获取还款方式为等额本金的每月偿还本金和利息
* *
* 公式每月偿还本息=贷款本金×月利率×(1月利率)还款月数÷(1月利率)还款月数-1 * 公式每月偿还本=(贷款本金÷还款月数)+(贷款本金-已归还本金累计额)×月利率
* *
* @param invest * @param invest
* 总借款额贷款本金 * 总借款额贷款本金
@ -23,19 +167,25 @@ public class AverageCapitalUtils {
* 还款总月数 * 还款总月数
* @return 每月偿还本金和利息,不四舍五入直接截取小数点最后两位 * @return 每月偿还本金和利息,不四舍五入直接截取小数点最后两位
*/ */
public static BigDecimal getPerMonthPrincipalInterest(double invest, double yearRate, int totalmonth) { public static Map<Integer, Double> getPerMonthPrincipalInterest(double invest, double yearRate, int totalMonth) {
Map<Integer, Double> map = new HashMap<Integer, Double>();
// 每月本金
double monthPri = getPerMonthPrincipal(invest, totalMonth);
// 获取月利率
double monthRate = yearRate / 12; double monthRate = yearRate / 12;
BigDecimal monthIncome = new BigDecimal(invest) monthRate = new BigDecimal(monthRate).setScale(6, BigDecimal.ROUND_DOWN).doubleValue();
.multiply(new BigDecimal(monthRate * Math.pow(1 + monthRate, totalmonth))) for (int i = 1; i <= totalMonth; i++) {
.divide(new BigDecimal(Math.pow(1 + monthRate, totalmonth) - 1), 2, BigDecimal.ROUND_UP); double monthRes = monthPri + (invest - monthPri * (i - 1)) * monthRate;
//return monthIncome.doubleValue(); monthRes = new BigDecimal(monthRes).setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
return monthIncome; map.put(i, monthRes);
}
return map;
} }
/** /**
* 等额本息计算获取还款方式为等额本息的每月偿还利息 * 等额本金计算获取还款方式为等额本金的每月偿还利息
* *
* 公式每月偿还利息=贷款本金×月利率×(1+月利率)^还款月数-(1+月利率)^(还款月序号-1)÷(1+月利率)^还款月数-1 * 公式每月应还利息=剩余本金×月利率=(贷款本金-已归还本金累计额)×月利率
* *
* @param invest * @param invest
* 总借款额贷款本金 * 总借款额贷款本金
@ -45,24 +195,24 @@ public class AverageCapitalUtils {
* 还款总月数 * 还款总月数
* @return 每月偿还利息 * @return 每月偿还利息
*/ */
public static Map<Integer, BigDecimal> getPerMonthInterest(double invest, double yearRate, int totalmonth) { public static Map<Integer, Double> getPerMonthInterest(double invest, double yearRate, int totalMonth) {
Map<Integer, BigDecimal> map = new HashMap<Integer, BigDecimal>(); Map<Integer, Double> inMap = new HashMap<Integer, Double>();
double monthRate = yearRate / 12; double principal = getPerMonthPrincipal(invest, totalMonth);
BigDecimal monthInterest; Map<Integer, Double> map = getPerMonthPrincipalInterest(invest, yearRate, totalMonth);
for (int i = 1; i < totalmonth + 1; i++) { for (Map.Entry<Integer, Double> entry : map.entrySet()) {
BigDecimal multiply = new BigDecimal(invest).multiply(new BigDecimal(monthRate)); BigDecimal principalBigDecimal = new BigDecimal(principal);
BigDecimal sub = new BigDecimal(Math.pow(1 + monthRate, totalmonth)) BigDecimal principalInterestBigDecimal = new BigDecimal(entry.getValue());
.subtract(new BigDecimal(Math.pow(1 + monthRate, i - 1))); BigDecimal interestBigDecimal = principalInterestBigDecimal.subtract(principalBigDecimal);
monthInterest = multiply.multiply(sub).divide(new BigDecimal(Math.pow(1 + monthRate, totalmonth) - 1), 2, interestBigDecimal = interestBigDecimal.setScale(2, BigDecimal.ROUND_DOWN);
BigDecimal.ROUND_DOWN); inMap.put(entry.getKey(), interestBigDecimal.doubleValue());
monthInterest = monthInterest.setScale(2, BigDecimal.ROUND_DOWN);
map.put(i, monthInterest);
} }
return map; return inMap;
} }
/** /**
* 等额本息计算获取还款方式为等额本息的每月偿还本金 * 等额本金计算获取还款方式为等额本金的每月偿还本金
*
* 公式每月应还本金=贷款本金÷还款月数
* *
* @param invest * @param invest
* 总借款额贷款本金 * 总借款额贷款本金
@ -72,22 +222,13 @@ public class AverageCapitalUtils {
* 还款总月数 * 还款总月数
* @return 每月偿还本金 * @return 每月偿还本金
*/ */
public static Map<Integer, BigDecimal> getPerMonthPrincipal(double invest, double yearRate, int totalmonth) { public static double getPerMonthPrincipal(double invest, int totalMonth) {
double monthRate = yearRate / 12; BigDecimal monthIncome = new BigDecimal(invest).divide(new BigDecimal(totalMonth), 2, BigDecimal.ROUND_DOWN);
BigDecimal monthIncome = new BigDecimal(invest) return monthIncome.doubleValue();
.multiply(new BigDecimal(monthRate * Math.pow(1 + monthRate, totalmonth)))
.divide(new BigDecimal(Math.pow(1 + monthRate, totalmonth) - 1), 2, BigDecimal.ROUND_DOWN);
Map<Integer, BigDecimal> mapInterest = getPerMonthInterest(invest, yearRate, totalmonth);
Map<Integer, BigDecimal> mapPrincipal = new HashMap<Integer, BigDecimal>();
for (Map.Entry<Integer, BigDecimal> entry : mapInterest.entrySet()) {
mapPrincipal.put(entry.getKey(), monthIncome.subtract(entry.getValue()));
}
return mapPrincipal;
} }
/** /**
* 等额本息计算获取还款方式为等额本息的总利息 * 等额本金计算获取还款方式为等额本金的总利息
* *
* @param invest * @param invest
* 总借款额贷款本金 * 总借款额贷款本金
@ -97,27 +238,16 @@ public class AverageCapitalUtils {
* 还款总月数 * 还款总月数
* @return 总利息 * @return 总利息
*/ */
public static double getInterestCount(double invest, double yearRate, int totalmonth) { public static double getInterestCount(double invest, double yearRate, int totalMonth) {
BigDecimal count = new BigDecimal(0); BigDecimal count = new BigDecimal(0);
Map<Integer, BigDecimal> mapInterest = getPerMonthInterest(invest, yearRate, totalmonth); Map<Integer, Double> mapInterest = getPerMonthInterest(invest, yearRate, totalMonth);
for (Map.Entry<Integer, BigDecimal> entry : mapInterest.entrySet()) { for (Map.Entry<Integer, Double> entry : mapInterest.entrySet()) {
count = count.add(entry.getValue()); count = count.add(new BigDecimal(entry.getValue()));
} }
return count.doubleValue(); return count.doubleValue();
} }
/**
* 应还本金总和
*
* @param invest
* 总借款额贷款本金
* @param yearRate
* 年利率
* @param month
* 还款总月数
* @return 应还本金总和
*/
public static double getPrincipalInterestCount(double invest, double yearRate, int totalmonth) { public static double getPrincipalInterestCount(double invest, double yearRate, int totalmonth) {
double monthRate = yearRate / 12; double monthRate = yearRate / 12;
BigDecimal perMonthInterest = new BigDecimal(invest) BigDecimal perMonthInterest = new BigDecimal(invest)
@ -131,25 +261,19 @@ public class AverageCapitalUtils {
/** /**
* @param args * @param args
*/ */
public static void main(String[] args) { public static void main() {
double invest = 38988; // 本金 double invest = 10000; // 本金
int month = 12; int month = 12;
double yearRate = 0.15; // 年利率 double yearRate = 0.015; // 年利率
BigDecimal perMonthPrincipalInterest = getPerMonthPrincipalInterest(invest, yearRate, month); Map<Integer, Double> getPerMonthPrincipalInterest = getPerMonthPrincipalInterest(invest, yearRate, month);
System.out.println("等额本息---每月还款本息:" + perMonthPrincipalInterest); System.out.println("等额本金---每月本息:" + getPerMonthPrincipalInterest);
System.out.println("等额本息---每月还款本息:" + getPerMonthPrincipalInterest(invest, yearRate, 3)); double benjin = getPerMonthPrincipal(invest, month);
System.out.println("等额本息---每月还款本息:" + getPerMonthPrincipalInterest(invest, yearRate, 6)); System.out.println("等额本金---每月本金:" + benjin);
System.out.println("等额本息---每月还款本息:" + getPerMonthPrincipalInterest(invest, yearRate, 9)); Map<Integer, Double> mapInterest = getPerMonthInterest(invest, yearRate, month);
System.out.println("等额本息---每月还款本息:" + getPerMonthPrincipalInterest(invest, yearRate, 12)); System.out.println("等额本金---每月利息:" + mapInterest);
System.out.println("等额本息---每月还款本息:" + getPerMonthPrincipalInterest(invest, yearRate, 15));
System.out.println("等额本息---每月还款本息:" + getPerMonthPrincipalInterest(invest, yearRate, 18));
/*Map<Integer, BigDecimal> mapInterest = getPerMonthInterest(invest, yearRate, month);
System.out.println("等额本息---每月还款利息:" + mapInterest);
Map<Integer, BigDecimal> mapPrincipal = getPerMonthPrincipal(invest, yearRate, month);
System.out.println("等额本息---每月还款本金:" + mapPrincipal);
double count = getInterestCount(invest, yearRate, month); double count = getInterestCount(invest, yearRate, month);
System.out.println("等额本---总利息:" + count); System.out.println("等额本金---总利息:" + count);
double principalInterestCount = getPrincipalInterestCount(invest, yearRate, month); double principalInterestCount = getPrincipalInterestCount(invest, yearRate, month);
System.out.println("等额本息---应还本息总和:" + principalInterestCount);*/ System.out.println("等额本金---应还本息总和:"+principalInterestCount);
}
} }
}