Microsoft Excel PMT function in Java

PMT, one of the financial functions, calculates the payment for a loan based on constant payments and a constant interest rate.

Syntax:
PMT(rate, nper, pv, [fv], [type])


The PMT function syntax has the following arguments:
  • Rate:- Required. The interest rate for the loan.
  • Nper:- Required. The total number of payments for the loan.
  • Pv:- Required. The present value, or the total amount that a series of future payments is worth now; also known as the principal.
  • Fv:- Optional. The future value, or a cash balance you want to attain after the last payment is made. If fv is omitted, it is assumed to be 0 (zero), that is, the future value of a loan is 0.
  • Type:- Optional. The number 0 (zero) or 1 and indicates when payments are due.

This can be represented in Java function as:

static public double pmt(double r, int nper, double pv, double fv, int type) {        double pmt = -r * (pv * Math.pow(1 + r, nper) + fv) / ((1 + r * type) * (Math.pow(1 + r, nper) - 1));        return pmt;    }


A high precision implementation using BigDecimal values is given as:

static public BigDecimal pmt(BigDecimal intRate, int nper, BigDecimal pv, BigDecimal fv, int type) {        BigDecimal numerator = intRate.multiply(((pv.multiply(ONE.add(intRate).pow(nper))).add(fv)));        BigDecimal denominator = (ONE.add(intRate.multiply(BigDecimal.valueOf(type)))).multiply(((ONE.add(intRate)).pow(nper)).subtract(ONE));        return numerator.divide(denominator, DECIMAL_SCALE, DECIMAL_ROUNDING_MODE).negate();    }

Source code can be found at: ExcelFunctions.java

0 comments:

Post a Comment