java中pmt计算_java - Java中的Excel PMT功能 - 堆栈内存溢出

这是我的尝试,主要是尝试使其具有与Excel相同的参数。 我从Apache POI删除了代码,并将其切换为使用BigDecimal 。

/**

* PMT function ported from Excel to Java to use BigDecimals.

* @param interestRate interest rate for the loan.

* @param numberOfPayments is the total number of payments for the loan.

* @param principal is the present value; also known as the principal.

* @param futureValue It is the future value, or the balance that you want to have left after the last payment. If fv is omitted, the fv is assumed to be zero.

* @param paymentsDueAtBeginningOfPeriod payments are due at the beginning of the period.

* @return payment

* @see FincanceLib

*/

public static BigDecimal pmt(BigDecimal interestRate,

int numberOfPayments,

BigDecimal principal,

BigDecimal futureValue,

boolean paymentsDueAtBeginningOfPeriod) {

final BigDecimal n = new BigDecimal(numberOfPayments);

if (BigDecimal.ZERO.equals(interestRate)) {

return (futureValue.add(principal)).divide(n, MathContext.DECIMAL128).negate();

} else {

final BigDecimal r1 = interestRate.add(BigDecimal.ONE);

final BigDecimal pow = r1.pow(numberOfPayments);

final BigDecimal divisor;

if (paymentsDueAtBeginningOfPeriod) {

divisor = r1.multiply(BigDecimal.ONE.subtract(pow));

} else {

divisor = BigDecimal.ONE.subtract(pow);

}

return (principal.multiply(pow).add(futureValue)).multiply(interestRate).divide(divisor, MathContext.DECIMAL128);

}

}

由于小数点相应地四舍五入,您可能会遇到问题。

@Test

public void testPMT() {

assertEquals(

new BigDecimal("-3756.00"),

pmt(

new BigDecimal("0.0075000"),

36,

new BigDecimal("119000.00"),

BigDecimal.ZERO,

true

).setScale(2, RoundingMode.HALF_UP)

);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值