原文:http://www.open-open.com/code/view/1449034309983

等额本息:

/**
* Description:等额本息工具类
* Copyright: Copyright (corporation)2015
* Company: Corporation
* @author: 凯文加内特
* @version: 1.0
* Created at: 2015年11月30日 下午3:45:46
* Modification History:
* Modified by :
*/ package com.utils; import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map; /**
* 等额本息还款,也称定期付息,即借款人每月按相等的金额偿还贷款本息,其中每月贷款利息按月初剩余贷款本金计算并逐月结清。把按揭贷款的本金总额与利息总额相加,
* 然后平均分摊到还款期限的每个月中。作为还款人,每个月还给银行固定金额,但每月还款额中的本金比重逐月递增、利息比重逐月递减。
*/ public class AverageCapitalPlusInterestUtils { /**
* 等额本息计算获取还款方式为等额本息的每月偿还本金和利息
*
* 公式:每月偿还本息=〔贷款本金×月利率×(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();
} /**
* 等额本息计算获取还款方式为等额本息的每月偿还利息
*
* 公式:每月偿还利息=贷款本金×月利率×〔(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);
}
}

等额本金:

/**
* Description:等额本金工具类
* Copyright: Copyright (Corporation)2015
* Company: Corporation
* @author: 凯文加内特
* @version: 1.0
* Created at: 2015年12月1日 上午8:38:23
* Modification History:
* Modified by :
*/ package com.utils; import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map; /**
* 等额本金是指一种贷款的还款方式,是在还款期内把贷款数总额等分,每月偿还同等数额的本金和剩余贷款在该月所产生的利息,这样由于每月的还款本金额固定,
* 而利息越来越少,借款人起初还款压力较大,但是随时间的推移每月还款数也越来越少。
*/
public class AverageCapitalUtils { /**
* 等额本金计算获取还款方式为等额本金的每月偿还本金和利息
*
* 公式:每月偿还本金=(贷款本金÷还款月数)+(贷款本金-已归还本金累计额)×月利率
*
* @param invest
* 总借款额(贷款本金)
* @param yearRate
* 年利率
* @param month
* 还款总月数
* @return 每月偿还本金和利息,不四舍五入,直接截取小数点最后两位
*/
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;
monthRate = new BigDecimal(monthRate).setScale(6, BigDecimal.ROUND_DOWN).doubleValue();
for (int i = 1; i <= totalMonth; i++) {
double monthRes = monthPri + (invest - monthPri * (i - 1)) * monthRate;
monthRes = new BigDecimal(monthRes).setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
map.put(i, monthRes);
}
return map;
} /**
* 等额本金计算获取还款方式为等额本金的每月偿还利息
*
* 公式:每月应还利息=剩余本金×月利率=(贷款本金-已归还本金累计额)×月利率
*
* @param invest
* 总借款额(贷款本金)
* @param yearRate
* 年利率
* @param month
* 还款总月数
* @return 每月偿还利息
*/
public static Map<Integer, Double> getPerMonthInterest(double invest, double yearRate, int totalMonth) {
Map<Integer, Double> inMap = new HashMap<Integer, Double>();
double principal = getPerMonthPrincipal(invest, totalMonth);
Map<Integer, Double> map = getPerMonthPrincipalInterest(invest, yearRate, totalMonth);
for (Map.Entry<Integer, Double> entry : map.entrySet()) {
BigDecimal principalBigDecimal = new BigDecimal(principal);
BigDecimal principalInterestBigDecimal = new BigDecimal(entry.getValue());
BigDecimal interestBigDecimal = principalInterestBigDecimal.subtract(principalBigDecimal);
interestBigDecimal = interestBigDecimal.setScale(2, BigDecimal.ROUND_DOWN);
inMap.put(entry.getKey(), interestBigDecimal.doubleValue());
}
return inMap;
} /**
* 等额本金计算获取还款方式为等额本金的每月偿还本金
*
* 公式:每月应还本金=贷款本金÷还款月数
*
* @param invest
* 总借款额(贷款本金)
* @param yearRate
* 年利率
* @param month
* 还款总月数
* @return 每月偿还本金
*/
public static double getPerMonthPrincipal(double invest, int totalMonth) {
BigDecimal monthIncome = new BigDecimal(invest).divide(new BigDecimal(totalMonth), 2, BigDecimal.ROUND_DOWN);
return monthIncome.doubleValue();
} /**
* 等额本金计算获取还款方式为等额本金的总利息
*
* @param invest
* 总借款额(贷款本金)
* @param yearRate
* 年利率
* @param month
* 还款总月数
* @return 总利息
*/
public static double getInterestCount(double invest, double yearRate, int totalMonth) {
BigDecimal count = new BigDecimal(0);
Map<Integer, Double> mapInterest = getPerMonthInterest(invest, yearRate, totalMonth); for (Map.Entry<Integer, Double> entry : mapInterest.entrySet()) {
count = count.add(new BigDecimal(entry.getValue()));
}
return count.doubleValue();
} /**
* @param args
*/
public static void main(String[] args) {
double invest = 10000; // 本金
int month = 12;
double yearRate = 0.15; // 年利率
Map<Integer, Double> getPerMonthPrincipalInterest = getPerMonthPrincipalInterest(invest, yearRate, month);
System.out.println("等额本金---每月本息:" + getPerMonthPrincipalInterest);
double benjin = getPerMonthPrincipal(invest, month);
System.out.println("等额本金---每月本金:" + benjin);
Map<Integer, Double> mapInterest = getPerMonthInterest(invest, yearRate, month);
System.out.println("等额本金---每月利息:" + mapInterest); double count = getInterestCount(invest, yearRate, month);
System.out.println("等额本金---总利息:" + count);
}
}

Java 等额本金等额本息工具类的更多相关文章

  1. java调用kettle的job和transfer工具类

    package com.woaiyitiaocai.util; import java.util.Map; import java.util.UUID; import org.apache.log4j ...

  2. Java Class与反射相关的一些工具类

    package com.opslab.util; import org.apache.log4j.Logger; import java.io.File;import java.io.IOExcept ...

  3. Java语言Lang包下常用的工具类介绍_java - JAVA

    文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 无论你在开发哪中 Java 应用程序,都免不了要写很多工具类/工具函数.你可知道,有很多现成的工具类可用,并且代码质量都 ...

  4. 【Java多线程】JUC包下的工具类CountDownLatch、CyclicBarrier和Semaphore

    前言 JUC中为了满足在并发编程中不同的需求,提供了几个工具类供我们使用,分别是CountDownLatch.CyclicBarrier和Semaphore,其原理都是使用了AQS来实现,下面分别进行 ...

  5. Java操作zip压缩和解压缩文件工具类

    需要用到ant.jar(这里使用的是ant-1.6.5.jar) import java.io.File; import java.io.FileInputStream; import java.io ...

  6. JAVA调用操作javascript (JS)工具类

    import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.FileReader;import ...

  7. Java基础知识强化92:日期工具类的编写和测试案例

    1. DateUtil.java,代码如下: package cn.itcast_04; import java.text.ParseException; import java.text.Simpl ...

  8. Java基础知识强化62:Arrays工具类之概述和使用

    1. Arrays工具类: Arrays这个类包含操作数组(比如排序和查找)的各种方法. 2. Arrays的方法: (1)toString方法:把数组转成字符串 public static Stri ...

  9. java中模拟http(https)请求的工具类

    在java中,特别是java web中,我们经常需要碰到的一个场景是我们需要从服务端去发送http请求,获取到数据,而不是直接从浏览器输入请求网址获得相应.比如我们想访问微信接口,获取其返回信息. 在 ...

随机推荐

  1. pickle 两个使用小方法

    def pickle_load(file_path): f = open(file_path,'r+') data = pickle.load(f) f.close() return data     ...

  2. vscode F12 不能用,原来是快捷键冲突了。

    vscode F12 不能用,原来是快捷键冲突了.

  3. 解决VSCode中使用vetur插件格式化vue文件时,js代码会自动加上冒号和分号

    解决VSCode中使用vetur插件格式化vue文件时,js代码会自动加上冒号和分号 在设置中把"vetur.format.defaultFormatter.js": " ...

  4. js 根据指定个数切割数组

    Part.1 问题 写项目时,遇到需要前端做 假分页 的问题:后端会将数据全部返回,前端自己做分页 Part.2 思路 拿到后端全部返回的数据后,按照 产品需求  进行分页,如每页显示 10 条数据为 ...

  5. STL || HDU 1894 String Compare

    如果一个词包含再另一个词的前面(前缀),是一对前缀,求一共有多少对 *解法:STL万岁 #include<string>:https://www.cnblogs.com/SZxiaochu ...

  6. python爬虫---从零开始(三)Requests库

    1,什么是Requests库 Requests是用python语言编写,基于urllib,采用Apache2 Licensed 开源协议的HTTP库. 它比urllib更加方便,可以节约我们大量的工作 ...

  7. 小程序01 微信小程序介绍和开发准备

    前言 火爆的微信小程序:跳一跳.摩拜单车.美柚.大众点评.滴滴出行 背景 为什么会有小程序? 微信最早推出公众号和服务号,公众号和服务号所传播的网页经常出现加载缓慢甚至页面空白的情况. 微信小程序的诞 ...

  8. IntelliJ IDEA使用eclipse compiler(ecj)解决lombok编译问题

    1:为什么要使用在idea使用eclipse compiler(ecj)? 因为idea默认使用javac,而javac当在编译时发现一个编译错误就直接停止编译了.而ecj支持proceed on e ...

  9. mybatis-4 mybatis与spring结合使用及原理

    1.创建项目maven,方便依赖下载.使用的jar如下: <dependencies> <dependency> <groupId>org.springframew ...

  10. mybatis-使用junit测试与main方法测试结果不一致问题

    今天使用ieda写mybatis程序感觉太不友好了,而且也没找到问题所在.问题:写的user.xml中的语句与输出的语句不一样.尝试了各种办法都没有解决. mybatis配置的使用的查询实体.xml ...