Microsoft Excel IPMT function in Java

Basic Description

The Excel IPMT function calculates the interest payment, during a specific period of a loan or investment that is paid in constant periodic payments, with a constant interest rate.

The syntax of the function is:
IPMT( rate, per, nper, pv, [fv], [type] )
Where the arguments are as follows:

rate - The interest rate, per period.
per - The period for which the interest payment is to be calculated (must be an integer between 1 and nper).
nper - The number of periods over which the loan or investment is to be paid.
pv - The present value of the loan / investment.
[fv] - An optional argument that specifies the future value of the loan / investment, at the end of nper payments. If omitted, [fv] takes on the default value of 0.
[type] - An optional argument that defines whether the payment is made at the start or the end of the period.
            The [type] argument can have the value 0 or 1, meaning:
            0 - The payment is made at the end of the period;
            1 - The payment is made at the start of the period.

            If the [type] argument is omitted, it takes on the default value of 0 (denoting payments made at the end of the period).

In Java this can be represented with following function:


static public double ipmt(double r, int per, int nper, double pv, double fv, int type) {        double ipmt = fv(r, per - 1, pmt(r, nper, pv, fv, type), pv, type) * r;        if (type == 1) ipmt /= (1 + r);        return ipmt;    }

A high precision function using BigDecimal is:


static public BigDecimal ipmt(BigDecimal intRate, int per, int nper, BigDecimal pv, BigDecimal fv, int type) {        BigDecimal ipmt = fv(intRate, per - 1, pmt(intRate, nper, pv, fv, type), pv, type).multiply(intRate);        if (type == 1) {            ipmt = ipmt.divide(BigDecimal.ONE.add(intRate), DECIMAL_SCALE, DECIMAL_ROUNDING_MODE);        }        return ipmt;    }

Full source code can be found at: ExcelFunctions.java

0 comments:

Post a Comment