@knockaway/loan-calculator v1.0.0
@knockaway/loan-calculator
The goal of this project is to make loan calculations as simple as using any one of the free online loan calculators. Just inputs and outputs!
Do you have an idea for a loan-calculator function? We welcome open source contributions!
Brought to you by the developers at Knock, who found the need to calculate APR, APY, and monthly payments for features related to the Knock Home Swap.
It may be used by both CJS and ESM module formats.
calculateApr
Calculates the APR (Annual Percentage Rate) of a loan with the given fees and structure.
import { calculateApr } from '@knockaway/loan-calculator';
const apr = calculateApr({
loanAmount: 320000,
interestRate: 0.03,
numberOfPayments: 360,
paymentFrequency: 12,
compoundingFrequency: 12,
financedFees: 4000,
upfrontFees: 1000
});
// apr ~ 0.03122Parameters
loanAmountAmount to be loaned, after down payment.interestRateRaw interest rate, e.g. 0.03125numberOfPaymentsNumber of payments in full loan term, e.g. 360 for a 30 Year Mortgage paid monthlypaymentFrequencyHow many times per year payments are made. Default: 12compoundingFrequencyHow many times per year interest compounds. Default: 12financedFeesFinanced fees are rolled into the loan and increase the monthly payment. Default: 0upfrontFeesUpfront fees effectively reduce the amount initially received in the loan instead of increasing monthly payment. Default: 0
calculateApy
Calculates the APY (Annual Percentage Yield) of a loan with the given interest rate.
import { calculateApy } from '@knockaway/loan-calculator';
const apy = calculateApy({
interestRate: 0.03122,
compoundingFrequency: 12
});
// apy ~ 0.03167Parameters
interestRateRaw interest rate, e.g. 0.03125compoundingFrequencyHow many times per year interest compounds. Default: 12
calculatePayment
Just like the PMT function of a spreadsheet, this function calculates the periodic payment needed to pay off the present loan balance after n periods of payments and compounding at the given interest rate.
import { calculatePayment } from '@knockaway/loan-calculator';
const pmt = calculatePayment({
presentValue: 320000,
interestRate: 0.03 / 12,
numberOfPayments: 360
});
// pmt ~ 1349.13Parameters
presentValuePresent value of the loaninterestRateRaw interest rate, e.g. 0.03125numberOfPaymentsNumber of payments in full loan term, e.g. 360 for a 30 Year Mortgage paid monthly
convertCompoundingFrequency
Finds the interest rate where compounding at the new annual frequency generates the same effective annual interest as the given interest rate at the old annual frequency.
This is best explained by an example. Suppose you have a loan with 4% annual interest compounding monthly but payments are made quarterly. The monthly interest rate is 0.04 / 12, which compounded 12 times gives an APY rate of (1 + 0.04 / 12)^12 - 1 == 0.04074. This function wlil determine the interest rate which can be compounded only 4 times (quarterly) and produce the same effective interest rate of 0.04074.
import { convertCompoundingFrequency } from '@knockaway/loan-calculator';
const rate = convertCompoundingFrequency({
interestRate: 0.06,
oldCompoundingFrequency: 12,
newCompoundingFrequency: 4
});
// rate ~ 0.0603Parameters
interestRateRaw interest rate, e.g. 0.03125oldCompoundingFrequencyHow many times per year interest compounds.newCompoundingFrequencyHow many times per year interest will compound after conversion.