Java实现等额本息
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;/*** 等额本息*/
public class EqualPrincipalInterestUtil {/*** 每期本金+利息* 公式:每月偿还本息=〔贷款本金×月利率×(1+月利率)^还款月数〕÷〔(1+月利率)^还款月数-1)* @param invest* 本金* @param periodRate* 每期利率* @param periodMonthNum* 期数* @return*/public static double getPerPeriodPrincipalInterest(double invest, double periodRate, int periodMonthNum) {BigDecimal monthIncome = new BigDecimal(invest).multiply(new BigDecimal(periodRate * Math.pow(1 + periodRate, periodMonthNum))).divide(new BigDecimal(Math.pow(1 + periodRate, periodMonthNum) - 1), 4, BigDecimal.ROUND_HALF_UP);return monthIncome.doubleValue();}/*** 每期利息* 公式:每月偿还利息=贷款本金×月利率×〔(1+月利率)^还款月数-(1+月利率)^(还款月序号-1)〕÷〔(1+月利率)^还款月数-1〕* @param invest* 本金* @param periodRate* 每期利率* @param periodMonthNum* 期数* @return*/public static Map<Integer, BigDecimal> getPerPeriodInterest(double invest, double periodRate, int periodMonthNum) {Map<Integer, BigDecimal> map = new HashMap<Integer, BigDecimal>();BigDecimal periodInterest;for (int i = 1; i < periodMonthNum + 1; i++) {BigDecimal multiply = new BigDecimal(invest).multiply(new BigDecimal(periodRate));BigDecimal sub = new BigDecimal(Math.pow(1 + periodRate, periodMonthNum)).subtract(new BigDecimal(Math.pow(1 + periodRate, i - 1)));periodInterest = multiply.multiply(sub).divide(new BigDecimal(Math.pow(1 + periodRate, periodMonthNum) - 1),6, BigDecimal.ROUND_HALF_UP);periodInterest = periodInterest.setScale(4, BigDecimal.ROUND_HALF_UP);map.put(i, periodInterest);}return map;}/*** 每期还款本金* * @param invest* 本金* @param periodRate* 每期利率* @param periodNum* 期数* @return*/public static Map<Integer, BigDecimal> getPerPeriodPrincipal(double invest, double periodRate, int periodNum) {BigDecimal monthIncome = new BigDecimal(invest).multiply(new BigDecimal(periodRate * Math.pow(1 + periodRate, periodNum))).divide(new BigDecimal(Math.pow(1 + periodRate, periodNum) - 1), 4, BigDecimal.ROUND_HALF_UP);Map<Integer, BigDecimal> mapInterest = getPerPeriodInterest(invest, periodRate, periodNum);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 periodRate* 每期利率* @param periodNum* 期数* @return*/public static double getInterestCount(double invest, double periodRate, int periodNum) {BigDecimal count = new BigDecimal(0);Map<Integer, BigDecimal> mapInterest = getPerPeriodInterest(invest, periodRate, periodNum);for (Map.Entry<Integer, BigDecimal> entry : mapInterest.entrySet()) {count = count.add(entry.getValue());}return count.doubleValue();}/*** 本息总和* * @param invest* 本金* @param periodRate* 每期利率* @param periodNum* 期数* @return*/public static double getPrincipalInterestCount(double invest, double periodRate, int periodNum) {BigDecimal perMonthInterest = new BigDecimal(invest);Map<Integer, BigDecimal> mapInterest = getPerPeriodInterest(invest, periodRate, periodNum);for (Map.Entry<Integer, BigDecimal> entry : mapInterest.entrySet()) {perMonthInterest = perMonthInterest.add(entry.getValue());}return perMonthInterest.doubleValue();}/*** 每期还款日期* @param start_date* 起租日* @param perPeriodMonthNum* 每期月数* @param periodNum* 期数* @return*/public static Map<Integer, String> getRepaymentDate(String start_date, int perPeriodMonthNum, int periodNum) {Map<Integer, String> periodRepaymentDate = new HashMap<Integer, String>();String nextRepaymentDate = start_date;periodRepaymentDate.put(1, nextRepaymentDate);for (int i = 2; i < periodNum + 1; i++) {nextRepaymentDate = getMonthAdd(perPeriodMonthNum, nextRepaymentDate, "yyyyMMdd");periodRepaymentDate.put(i, nextRepaymentDate);}return periodRepaymentDate;}/*** 功能描述:返回指定日期加上多少月之后的时间<BR>* @param from yyyyMMdd* @param day* @param formatStr* @return*/public static String getMonthAdd(int day,String from,String formatStr){SimpleDateFormat sdf=new SimpleDateFormat(formatStr);Calendar calendar = Calendar.getInstance();try {calendar.setTime(sdf.parse(from));} catch (Exception e) {e.printStackTrace();}calendar.add(Calendar.MONTH, day);String date = sdf.format(calendar.getTime());return date;}public static void main(String[] args) { double invest = 10000; // 本金 int periodNum = 4;double periodRate = 0.12/3; // 年利率 System.out.println("等额本息---本金:" + invest); double perMonthPrincipalInterest = getPerPeriodPrincipalInterest(invest, periodRate, periodNum); System.out.println("等额本息---每期还款本息:" + perMonthPrincipalInterest); Map<Integer, BigDecimal> mapInterest = getPerPeriodInterest(invest, periodRate, periodNum); System.out.println("等额本息---每期还款利息:" + mapInterest); Map<Integer, BigDecimal> mapPrincipal = getPerPeriodPrincipal(invest, periodRate, periodNum); System.out.println("等额本息---每期还款本金:" + mapPrincipal); double count = getInterestCount(invest, periodRate, periodNum); System.out.println("等额本息---总利息:" + count); double principalInterestCount = getPrincipalInterestCount(invest, periodRate, periodNum); System.out.println("等额本息---应还本息总和:" + principalInterestCount); }
}
Java实现等额本息相关推荐
- Java之~ 等额本息,等额本金,组合贷
一,等额本息工具类 import java.math.BigDecimal; import java.text.DecimalFormat; import java.util.HashMap; imp ...
- 等额本金,等额本息,随借随还,利随本清,按月付息到期还本,5种还款方式java计算方法
等额本金,等额本息,随借随还,利随本清,按月付息到期还本,5种还款方式java计算方法 等额本息定义:本金逐月递增,利息逐月递减,月还款数不变. 等额本金定义:本金保持相同,利息逐月递减,月还款数递减 ...
- java 等额本金等额本息工具类
2019独角兽企业重金招聘Python工程师标准>>> 等额本息: /*** Description:等额本息工具类* Copyright: Copyright (corporati ...
- 等额本息贷款 ——已知贷款本金、月还款额、贷款月数,反推贷款月利率、年利率-java实现
等额本息 每月还款计算公式: 每月本息金额 = (本金×月利率×(1+月利率)^还款月数)÷ ((1+月利率)^还款月数-1)) 反转求出 月利率 月利率 如果根据上面公式反转是算不出来的. 下面给出 ...
- Java实现:房贷计算器 (关键词:公积金、首付、等额本息、等额本金)
1 背景:房贷计算器 目前现有的网页 房贷计算器,多多少少有点不符合自己需求,因此根据房贷相关公式及 java编写一个贷款计算器现有代码,改写了符合自己要求的 房贷计算器 . 计算结果精度为小数点后2 ...
- 贷款计算器- 等额本金、等额本息工具类(Java版)
/** * Description:等额本金工具类 * Copyright: Copyright (Corporation)2015 * Company: Corporation * @version ...
- Java Swing写简单计算器以及拓展贷款计算器(等额本息和等额本金计算可以单独拿出来用)
1. 简单计算器 纯手写 package com.jisuanqi;import com.sun.xml.internal.ws.util.StringUtils;import javax.swing ...
- 等额本金、等额本息工具类(Java版)
等额本息: /** * Description:等额本息工具类 * Copyright: Copyright (corporation)2015 * Company: Corporation * @a ...
- java 模拟贷款实现等额本息还款
还是菜鸟的我,在学习完java面向对象的思想后编写了一个模拟贷款的程序!! 一.首先创建一个类用于给变量赋值和输出. 代码如下: public class Loan {private double m ...
最新文章
- 输入框中默认的值,判断是否输入内容
- python小游戏代码大全-python小游戏实现代码
- 使用 gperf 实现高效的 C/C++ 命令行处理
- SAP面向iOS设备推Cloud Platform SDK工具
- cf1555A. PizzaForces
- 前端学习(3268):js中this在类中的表现
- Linux内存管理:反向映射机制(匿名页,文件页和ksm页)
- 理解Dubbo的调用流程与Dubbo多协议解析
- linux 信号集 同步,linux信号集
- ERP九大流程图(图)
- C++求解一元二次方程
- python爬虫 获取学信网 学校与对应专业信息
- 生成树协议三姐妹:STP、RSTP 和 MSTP,附思科和华为双厂商命令示例
- SpringBoot JPA多对一 持久化是报错object references an unsaved transient instance - save the transient instanc
- 计算机打开查看方式默认是什么样,如何设置电脑文件夹默认查看方式
- 分布式事务seat对于sql语句的语法要求
- 小米盒子3增强版 android tv,小米盒子3增强版刷Android TV系统
- 最全的linux上git教程
- C语言 交替符号累加计算
- 【转】手机软件商店(wiki)