Microsoft Excel FV function in Java

FV, one of the financial functions, calculates the future value of an investment based on a constant interest rate. You can use FV with either periodic, constant payments, or a single lump sum payment.

Syntax
FV(rate,nper,pmt,[pv],[type])
The syntax has the following arguments:

Rate:- Required. The interest rate per period.
Nper:- Required. The total number of payment periods in an annuity.
Pmt:- Required. The payment made each period; it cannot change over the life of the annuity. Typically, pmt contains principal and interest but no other fees or taxes. If pmt is omitted, you must include the pv argument.
Pv:- Optional. The present value, or the lump-sum amount that a series of future payments is worth right now. If pv is omitted, it is assumed to be 0 (zero), and you must include the pmt argument.
Type:- Optional. The number 0 or 1 and indicates when payments are due. If type is omitted, it is assumed to be 0.
           0 - At the end of the period
           1 - At the beginning of the period

The Java equivalent function is:


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

A high precision version using BigDecimal will be:


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

Full source code can be found at: ExcelFunctions.java

Mircrosoft Excel PPMT function in Java

Basic Description

The Excel PPMT function calculates the payment on the principal, 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:
PPMT( rate, per, nper, pv, [fv], [type] )
Where the arguments are as follows:

rate - The interest rate, per period.
per - The period for which the payment on the principal 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 specifies 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).

The function can be represented in Java as:


static public double ppmt(double r, int per, int nper, double pv, double fv, int type) {        return pmt(r, nper, pv, fv, type) - ipmt(r, per, nper, pv, fv, type);    }

Full source code can be found at: ExcelFunctions.java

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

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