BigDecimal用法之计算等额本金和等额本息
一.引言
《EffactiveJava》中介绍float和double类型的主要设计目标是为了科学计算和工程计算。他们执行二进制浮点运算,这是为了在广域数值范围上提供较为精确的快速近似计算而精心设计的。然而,它们没有提供完全精确的结果,所以不应该被用于要求精确结果的场合。但是,商业计算往往要求结果精确,这时候BigDecimal就派上大用场啦。
二.常用方法
add(BigDecimal) BigDecimal对象中的值相加,然后返回这个对象。
subtract(BigDecimal) BigDecimal对象中的值相减,然后返回这个对象。
multiply(BigDecimal) BigDecimal对象中的值相乘,然后返回这个对象。
divide(BigDecimal) BigDecimal对象中的值相除,然后返回这个对象。
toString() 将BigDecimal对象的数值转换成字符串。
doubleValue() 将BigDecimal对象中的值以双精度数返回。
floatValue() 将BigDecimal对象中的值以单精度数返回。
longValue() 将BigDecimal对象中的值以长整数返回。
intValue() 将BigDecimal对象中的值以整数返回。
BigDecimal.ROUND_UP 最后一位如果大于0,则向前进一位,正负数都如此。 BigDecimal.ROUND_DOWN 最后一位不管是什么都会被舍弃。 BigDecimal.ROUND_CEILING 如果是正数,按ROUND_UP处理,如果是负数,按照ROUND_DOWN处理。例如7.1->8; -7.1->-7;所以这种近似的结果都会>=实际值。 BigDecimal.ROUND_FLOOR 跟BigDecimal_ROUND_CEILING相反。例如7.1->7;-7.1->-8。这种处理的结果<=实际值。 BigDecimal.ROUND_HALF_DOWN 如果最后一位<=5则舍弃,如果>5, 向前进一位。如7.5->7;7.6->8;-7.5->-7 BigDecimal.ROUND_HALF_UP 如果最后一位<5则舍弃,如果>=5, 向前进一位。反之舍弃。如7.5->8;7.4->7;-7.5->-8 BigDecimal.ROUND_HALF_EVEN 如果倒数第二位是奇数,按照BigDecimal.ROUND_HALF_UP处理,如果是偶数,按照BigDecimal.ROUND_HALF_DOWN来处理。如7.5->8;8.5->8;7.4->7;-7.5->-8
注意事项:
(1)商业计算使用BigDecimal。
(2)尽量使用参数类型为String的构造函数。
(3) BigDecimal都是不可变的(immutable)的,在进行每一步运算时,都会产生一个新的对象,所以在做加减乘除运算时千万要保存操作后的值。
三.利用BigDecimal计算等额本息:
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); }
}
四.利用BigDecimal计算等额本金:
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); }
}
BigDecimal用法之计算等额本金和等额本息相关推荐
- Java计算等额本金和等额本息
Java计算等额本金和等额本息 等额本金 等额本息 等额本金 /*** 等额本金是指一种贷款的还款方式,是在还款期内把贷款数总额等分,每月偿还同等数额的本金和剩余贷款在该月所产生的利息,这样由于每月的 ...
- [转]详细说明等额本金和等额本息的计算方式与利弊
关于利率表请查看以下的帖子,这里只给举出例子,相信大家看后都可以自己计算了. 注: 1.贷款年限5年以上的年利率一律为5.4%. 2.月利率等额本金与等额本息的都一样为0.0042. 3.以贷款20万 ...
- 基于“总价、首付、贷款年限、利息、贷款额度”信息,计算每月最低还款额度。需要能按“等额本金、等额本息”两种形式计算还款
#include <stdio.h> void debj(); void debx(); int main() {int n;while(1){printf("0退出\n1等额本 ...
- 等额本金,等额本息,随借随还,利随本清,按月付息到期还本,5种还款方式java计算方法
等额本金,等额本息,随借随还,利随本清,按月付息到期还本,5种还款方式java计算方法 等额本息定义:本金逐月递增,利息逐月递减,月还款数不变. 等额本金定义:本金保持相同,利息逐月递减,月还款数递减 ...
- 等额本金、等额本息,两者到底怎么区分?
等额本金和等额本息是房贷最常见的两种还款方式,但两者却有很多差别,而且很多人对如何区分等额本息和等额本金云里雾里,傻傻分不清. 其实要区分等额本金和等额本息也很简单. 首先我们来说下等额本金. 所谓等 ...
- 房贷利率有无套路?Python解读“等额本金与等额本息”的差异所在
作者 | xiaoyu 来源 | Python数据科学 很多朋友留言说不知道房贷利率的不同归还方式是不是有套路,内心深表疑虑.我的第一反应是因为房贷很高,大家看到消费分期的套路自然而然就想到了房贷,很 ...
- 房贷等额本金和等额本息有什么区别,为何很多人傻傻分不清?
等额本金和等额本息是房贷最常见的两种还款方式,但两者却有很多差别,而且很多人对如何区分等额本息和等额本金云里雾里,傻傻分不清. 其实要区分等额本金和等额本息也很简单. 首先我们来说下等额本金. 所谓等 ...
- 等额本金和等额本息两种贷款方式的比较
等额本金和等额本息两种贷款方式的对比 本文介绍两种还款方式:等额本金.等额本息.通过分析认为等额本金的偿还方式更加划算. 等额本金:每个月偿还相同本金分额,并且支付上个月剩余本金产生的利息. 等额本息 ...
- 等额本金和等额本息是怎么算出来的
今天的问题与大家的日常生活紧密相关.如果你购买房子,通常会有银行贷款.银行计算贷款利息的主要方式有两种:等额本金和等额本息. 等额本金,是指每个月你还固定数量的本金,利息则根据贷款余额和月利率另算.比 ...
- 贷款还款方式(等额本金,等额本息,等本等息,先息后本)
等额本金 月还款总额逐月降低 月还款本金固定,为贷款总金额÷期数 月还款利息逐渐降低,为每月剩余贷款余额×单期利息 图例: 本金10000 月息1% 贷款12期 月还款本金=10000÷12=833. ...
最新文章
- 用简单术语让你看到贝叶斯优化之美
- java futher多线程_Java多线程系列--“JUC集合”05之 ConcurrentSkipListMap
- (转)Engineering Productivity
- python3 装饰器_python3装饰器
- apache下IE6对js的bug处理
- Eclipse报错:this compilation unit is not on the build path of a java project
- leetcode题解54-螺旋矩阵
- 相似文档查找算法之 simHash
- 采用单/双缓冲区需要花费的时间
- 保定华电计算机考研真题,华北电力大学(保定)离散数学试题(含2005年真题)...
- 英雄联盟android,安卓ARPG佳作 《英雄联盟(League of Heroes)》
- 【刷题-每天一算法】赛马
- 台式机装苹果系统_关于最近折腾黑苹果的一些心得分享
- python通讯录管理系统
- Unity 接入Apple登录
- 爱普生CH-TW5700T和爱普生CH-TW5800T好不好?配置怎么样?
- 嵌入式软件工程师都在开发什么?
- 当年高考,小灰是怎么混过来的?
- 如何查找session过期时间
- PS动作组织工具:ActionStation2 for mac