A JavaScript library of common financial functions to be used in time value of money calculations.
npm install tvm-financejs  
npm install tvm-financejs --save
var Finance = require("tvm-financejs");
var finance = new Finance();
// To calculate a payment:
Math.round(finance.PMT(.0525, 5, -10000) * 100) / 100;
// Returns 2325.73
`
To see these formulas in use, please visit tvm-calculator.
Available Functions
$3
- Just like Excel, I do not add rounding to the outputs. In many cases, you may want to use these formulas in combination with each other, in which case a rounded output will degrade the accuracy of the final values.
- Inputs in '[ ]' are optional for the formula.
- PV is typically represented as a negative value in the inputs/outputs.
- Rate must be represented in equivalent format to the periods. (e.g. if the APR is 5% but the periods are monthly, you need to divide the rate by 12).
$3
Variable | Description
--- | ---
pv | present value
fv | future value
pmt | payment
nper | total number of periods
per | a specific period, used in IPMT & PPMT
rate | rate for the period(s)
type | when payments are due (0 for end of period/arrears, and 1 for beginning of period/advance)
guess | a guess at the rate, optional value for the RATE formula
values | a set of periodic cash flows
$3
finance.PV(rate, nper, pmt, [fv], [type]);
Returns the present value of an investment, or the total amount that a series of future payments is worth now.
$3
finance.FV(rate, nper, pmt, pv, [type]);
Returns the future value of an investment based on periodic, equal, payments and a constant interest rate.
$3
finance.PMT(rate, nper, pv, [fv], [type]);
Calculates the payment for a loan based on a constant stream of equal payments and a constant interest rate.
$3
finance.IPMT(rate, per, nper, pv, [fv], [type]);
Returns the calculated interest portion of a payment for a specific period based on a constant stream of equal payments and a constant interest rate.
$3
finance.PPMT(rate, per, nper, pv, [fv\], [type]);
Returns the calculated principal portion of a payment for a specific period based on a constant stream of equal payments and a constant interest rate.
$3
finance.RATE(nper, pmt, pv, [fv], [type], [guess]);
Returns the interest rate per period for a loan or investment.
$3
finance.NPV(rate, value1, [value2], ... [valueN]);
Returns the net present value of an investment based on a constant rate of return and a series of future payments/investments (as negative values) and income/return (as positive values).
$3
finance.IRR(values, [guess]);
Returns the internal rate of return for a series of cash flows.
A couple of items to note about this formula:
- The variable values must be input as an array.
- There must be at least one negative and one positive value as part of the cash flow.
- Cash flows are assumed to be due in the same order they are arranged in the Array.
Example usage:
`
returnIRR() {
const values = [-1500, 500, 500, 500, 500];
return Math.round(finance.IRR(values) 100 ) / 100 100;
}
// returns 12.59
`
Contributing
Feel free to contribute and add more formulas to the library. Please try to add comments explaining the math behind the functions as they are added, and ensure unit tests are added/updated appropriately. The library is set up to use Jest for unit testing.
Testing
Each finance formula has a single test comprised of a number of sub-tests. (I wanted to run hundreds of tests but didn't want to scroll through that many results!). The tests are designed to either:
- Match the equivalent Excel formula to 8 decimal places; or
- Trigger a specific error message.
Test scenarios comprise a variety of terms, periods, types, and inputs.
To run the test suites:
npm test`