java中每月等额与先息后本计算
程序员文章站
2024-03-04 09:33:23
一般信用贷款会提供两种还款方式:每月等额或者先息后本。每月等额,就是每月归还等同的部分本金和利息,你手里在使用的本金其实是逐月减少的。先息后本就是先还利息,到期归还本金。...
一般信用贷款会提供两种还款方式:每月等额或者先息后本。每月等额,就是每月归还等同的部分本金和利息,你手里在使用的本金其实是逐月减少的。先息后本就是先还利息,到期归还本金。
每月等额
import java.math.bigdecimal; import java.util.calendar; import java.util.date; /** * <p>title: 等额本息还款工具类</p> * */ public class cpmutils{ /** * <p>description: 每月还款总额。〔贷款本金×月利率×(1+月利率)^还款月数〕÷〔(1+月利率)^还款月数-1〕</p> * @param principal 贷款本金 * @param monthlyinterestrate 月利率 * @param amount 期数 * @return */ public static bigdecimal monthlyrepayment(bigdecimal principal, bigdecimal monthlyinterestrate, int amount){ //(1+月利率)^还款月数 bigdecimal temp = monthlyinterestrate.add(moneyutils.one).pow(amount); return principal.multiply(monthlyinterestrate) .multiply(temp) .divide(temp.subtract(moneyutils.one), moneyutils.mathcontext); } /** * <p>description: 月还款利息。(贷款本金×月利率-月还款额)*(1+月利率)^(当前期数-1)+月还款额</p> * @param principal 贷款本金 * @param monthlyinterestrate 月利率 * @param monthlyrepayment 月还款额 * @param number 当前期数 * @return */ public static bigdecimal monthlyinterest(bigdecimal principal, bigdecimal monthlyinterestrate, bigdecimal monthlyrepayment, int number){ //(1+月利率)^(当前期数-1) bigdecimal temp = monthlyinterestrate.add(moneyutils.one).pow(number - 1); return principal.multiply(monthlyinterestrate) .subtract(monthlyrepayment) .multiply(temp).add(monthlyrepayment, moneyutils.mathcontext); } /** * <p>description: 还款总利息。期数×贷款本金×月利率×(1+月利率)^期数÷〔(1+月利率)^期数-1〕-贷款本金 </p> * @param principal 贷款本金 * @param monthlyinterestrate 月利率 * @param amount 还款期数 * @return */ public static bigdecimal interest(bigdecimal principal, bigdecimal monthlyinterestrate, int amount){ //(1+月利率)^期数 bigdecimal temp = monthlyinterestrate.add(moneyutils.one).pow(amount); return new bigdecimal(amount) .multiply(principal) .multiply(monthlyinterestrate) .multiply(temp) .divide(temp.subtract(moneyutils.one), moneyutils.mathcontext) .subtract(principal, moneyutils.mathcontext); } /** * <p>description: 月还款本金。已经精确到分位,未做单位换算</p> * @param principal 贷款本金 * @param monthlyinterestrate 月利率 * @param monthlyrepayment 月还款额 * @param number 当前期数 * @return */ public static bigdecimal monthlyprincipal(bigdecimal principal, bigdecimal monthlyinterestrate, bigdecimal monthlyrepayment, int number){ bigdecimal monthinterest = monthlyinterest(principal, monthlyinterestrate, monthlyrepayment, number); //月还款额-月还款利息 return monthlyrepayment.subtract(monthinterest).setscale(moneyutils.moneyshowscale, moneyutils.saveroundingmode); } /** * <p>description: 月还款本金。已经精确到分位,未做单位换算</p> * @param monthrepayment 月还款总额 * @param monthinterest 月还款利息 * @return */ public static bigdecimal monthprincipal(bigdecimal monthrepayment, bigdecimal monthinterest){ //月还款总额-月还款利息 return monthrepayment.subtract(monthinterest).setscale(moneyutils.moneyshowscale, moneyutils.saveroundingmode); } }
先息后本
import java.math.bigdecimal; /** * <p>title: 先息后本还款方式工具类型</p> */ public class biapputils extends repaymentutils { /** * <p>description: 月还款利息 贷款本金×月利率 </p> * @param loan 贷款本金 * @param monthlyinterestrate 月利率 * @return */ public static bigdecimal monthlyinterest(bigdecimal loan, bigdecimal monthlyinterestrate){ return loan.multiply(monthlyinterestrate, moneyutils.mathcontext); } /** * <p>description: 还款总利息 贷款本金×月利率×期数</p> * @param loan 贷款本金 * @param monthlyinterestrate 月利率 * @param number 期数 * @return */ public static bigdecimal interest(bigdecimal loan, bigdecimal monthlyinterestrate, int number){ return loan.multiply(monthlyinterestrate).multiply(new bigdecimal(number), moneyutils.mathcontext); } /** * <p>description: 月还款额</p> * @param loan 贷款本金 * @param monthlyinterestrate 月利率 * @param amount 期数 * @param curnumber 当前期数 * @return */ public static bigdecimal monthlyrepayment(bigdecimal loan, bigdecimal monthlyinterestrate, int amount, int curnumber){ bigdecimal monthlyinterest = monthlyinterest(loan, monthlyinterestrate); if(amount == curnumber){ return monthlyinterest.add(loan, moneyutils.mathcontext);//最后月还款额 }else{ return monthlyinterest; } } }
*金额计算工具类
import java.math.bigdecimal; import java.math.mathcontext; import java.math.roundingmode; import java.text.numberformat; public class moneyutils { /** * 标度(小数位数) */ public static final int scale = 10; /** * 金钱显示标度(小数位数) */ public static final int moneyshowscale = 2; /** * 利率显示标度(小数位数) */ public static final int interestrateshowscale = 4; /** * 精度 */ public static final int precision = 30; /** * 保存舍入规则 */ public static final roundingmode saveroundingmode = roundingmode.half_up; /** * 是否舍去小数点最后的零 */ public static boolean striptrailingzeros = true; /** * 运算上下文(设置精度、舍入规则) */ public static final mathcontext mathcontext = new mathcontext(precision, saveroundingmode); /** * 每年天数 */ public static final string yeardays = "360"; /** * 每年月数 */ public static final string yearmoths = "12"; /** * 每月天数 */ public static final string mothdays = "30"; /** * 数字“1” */ public static final bigdecimal one = new bigdecimal(1); /** * 数字“100” */ public static final bigdecimal hundred = new bigdecimal(100); /** * 数字“0.01” */ public static final bigdecimal onehundredth = new bigdecimal(0.01); public static bigdecimal newbigdecimal(string str){ return (str == null || str.trim().isempty()) ? bigdecimal.zero : new bigdecimal(str); } /** * <p>description: 加法返回格式化结果数字</p> * @param addend * @param augend * @return */ public static bigdecimal add(bigdecimal addend, bigdecimal augend){ return formatmoney(addend.add(augend, mathcontext)); } /** * <p>description: 加法返回格式化结果数字</p> * @param addend * @param augend * @return */ public static bigdecimal add(string addend, string augend){ bigdecimal decimaladdend = newbigdecimal(addend); bigdecimal decimalaugend = newbigdecimal(augend); return formatmoney(decimaladdend.add(decimalaugend, mathcontext)); } /** * <p>description: 加法返回格式化结果字符串</p> * @param addend * @param augend * @return */ public static string addtostring(bigdecimal addend, bigdecimal augend){ return formattostring(addend.add(augend, mathcontext)); } /** * <p>description: 加法返回格式化结果字符串</p> * @param addend * @param augend * @return */ public static string addtostring(string addend, string augend){ bigdecimal decimaladdend = newbigdecimal(addend); bigdecimal decimalaugend = newbigdecimal(augend); return formattostring(decimaladdend.add(decimalaugend, mathcontext)); } /** * <p>description: 减法返回格式化结果数字</p> * @param minuend * @param subtrahend * @return */ public static bigdecimal subtract(bigdecimal minuend, bigdecimal subtrahend){ return formatmoney(minuend.subtract(subtrahend, mathcontext)); } /** * <p>description: 减法返回格式化结果数字</p> * @param minuend * @param subtrahend * @return */ public static bigdecimal subtract(string minuend, string subtrahend){ bigdecimal decimalminuend = newbigdecimal(minuend); bigdecimal decimalsubtrahend = newbigdecimal(subtrahend); return formatmoney(decimalminuend.subtract(decimalsubtrahend, mathcontext)); } /** * <p>description: 减法返回格式化结果字符串</p> * @param minuend * @param subtrahend * @return */ public static string subtracttostring(bigdecimal minuend, bigdecimal subtrahend){ return formattostring(minuend.subtract(subtrahend, mathcontext)); } /** * <p>description: 减法返回格式化结果字符串</p> * @param minuend * @param subtrahend * @return */ public static string subtracttostring(string minuend, string subtrahend){ bigdecimal decimalminuend = newbigdecimal(minuend); bigdecimal decimalsubtrahend = newbigdecimal(subtrahend); return formattostring(decimalminuend.subtract(decimalsubtrahend, mathcontext)); } /** * <p>description: 乘法返回格式化结果数字</p> * @param multiplier * @param multiplicand * @return */ public static bigdecimal multiply(bigdecimal multiplier, bigdecimal multiplicand){ return formatmoney(multiplier.multiply(multiplicand, mathcontext)); } /** * <p>description: 乘法返回格式化结果数字</p> * @param multiplier * @param multiplicand * @return */ public static bigdecimal multiply(string multiplier, string multiplicand){ bigdecimal decimalmultiplier = newbigdecimal(multiplier); bigdecimal decimalmultiplicand = newbigdecimal(multiplicand); return formatmoney(decimalmultiplier.multiply(decimalmultiplicand, mathcontext)); } /** * <p>description: 乘法返回格式化结果字符串</p> * @param multiplier * @param multiplicand * @return */ public static string multiplytostring(bigdecimal multiplier, bigdecimal multiplicand){ return formattostring(multiplier.multiply(multiplicand, mathcontext)); } /** * <p>description: 乘法返回格式化结果字符串</p> * @param multiplier * @param multiplicand * @return */ public static string multiplytostring(string multiplier, string multiplicand){ bigdecimal decimalmultiplier = newbigdecimal(multiplier); bigdecimal decimalmultiplicand = newbigdecimal(multiplicand); return formattostring(decimalmultiplier.multiply(decimalmultiplicand, mathcontext)); } /** * <p>description: 除法返回格式化结果数字</p> * @param dividend * @param divisor * @return */ public static bigdecimal divide(bigdecimal dividend, bigdecimal divisor){ return formatmoney(dividend.divide(divisor, mathcontext)); } /** * <p>description: 除法返回格式化结果数字</p> * @param dividend * @param divisor * @return */ public static bigdecimal divide(string dividend, string divisor){ bigdecimal decimaldividend = newbigdecimal(dividend); bigdecimal decimaldivisor = newbigdecimal(divisor); return formatmoney(decimaldividend.divide(decimaldivisor, mathcontext)); } /** * <p>description: 除法返回格式化结果字符串</p> * @param dividend * @param divisor * @return */ public static string dividetostring(bigdecimal dividend, bigdecimal divisor){ return formattostring(dividend.divide(divisor, mathcontext)); } /** * <p>description: 除法返回格式化结果字符串</p> * @param dividend * @param divisor * @return */ public static string dividetostring(string dividend, string divisor){ bigdecimal decimaldividend = newbigdecimal(dividend); bigdecimal decimaldivisor = newbigdecimal(divisor); return formattostring(decimaldividend.divide(decimaldivisor, mathcontext)); } /** * <p>description: 月利率计算</p> * @param yearinterestrate * @return */ public static bigdecimal monthinterestrate(bigdecimal yearinterestrate){ bigdecimal dayinterestrate = moneyutils.divide(yearinterestrate, yeardays).setscale(5, roundingmode.ceiling); system.err.println(dayinterestrate); bigdecimal monthinterestrate = dayinterestrate.multiply(newbigdecimal(mothdays)); system.err.println(monthinterestrate); return monthinterestrate; } /** * <p>description: 按既定小数位数格式化金额保存</p> * @param result * @return */ public static bigdecimal formatmoney(bigdecimal result){ return result.setscale(scale, saveroundingmode); } /** * <p>description: 按既定小数位数格式化金额显示</p> * @param resultstr 要格式化的数 * @param multiple 乘以的倍数 * @return */ public static string formatmoneytoshow(string resultstr, bigdecimal multiple){ bigdecimal result = newbigdecimal(resultstr); return moneyutils.formattostring(moneyutils.formatmoneytoshow(result, multiple)); } /** * <p>description: 按既定小数位数格式化金额显示</p> * @param result 要格式化的数 * @param multiple 乘以的倍数 * @return */ public static bigdecimal formatmoneytoshow(bigdecimal result, bigdecimal multiple){ return result.multiply(multiple).setscale(moneyshowscale, saveroundingmode); } /** * <p>description: 按既定小数位数格式化利率显示</p> * @param result 要格式化的数 * @param multiple 乘以的倍数 * @return */ public static bigdecimal formatinterestratetoshow(bigdecimal result, bigdecimal multiple){ return result.multiply(multiple).setscale(interestrateshowscale, saveroundingmode); } /** * <p>description: 按既定小数位数格式化显示</p> * @param result 要格式化的数 * @param scale 显示标度(小数位数) * @return */ public static bigdecimal formattoshow(bigdecimal result, int scale){ return result.setscale(scale, saveroundingmode); } /** * <p>description: 格式化为字符串,进行去零不去零操作</p> * @param result * @return */ public static string formattostring(bigdecimal result){ if(result == null){ return ""; }else{ return striptrailingzeros ? result.striptrailingzeros().toplainstring() : result.toplainstring(); } } /** * <p>description: 按既定小数位数格式化为货币格式</p> * @param result * @return */ public static string formattocurrency(bigdecimal result){ bigdecimal temp = result.divide(hundred, saveroundingmode); numberformat numberformat = numberformat.getcurrencyinstance(); return numberformat.format(striptrailingzeros ? temp.striptrailingzeros() : temp); } public static string formattopercent(bigdecimal result){ bigdecimal temp = result.divide(hundred, saveroundingmode); numberformat numberformat = numberformat.getpercentinstance(); return numberformat.format(striptrailingzeros ? temp.striptrailingzeros() : temp); } /** * <p>description:格式化数字为千分位显示; </p> * @param text * @return */ public static string fmtmicrometer(string text){ decimalformat df = null; if(text.indexof(".") > 0) { if(text.length() - text.indexof(".")-1 == 0){ df = new decimalformat("###,##0."); }else if(text.length() - text.indexof(".")-1 == 1){ df = new decimalformat("###,##0.0"); }else { df = new decimalformat("###,##0.00"); } }else{ df = new decimalformat("###,##0.00"); } double number = 0.0; try { number = double.parsedouble(text); } catch (exception e) { number = 0.0; } return df.format(number); } }
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!