# Loan payment calculator

Enter an amount, an annual rate and a term in months to see the monthly payment, the total interest and the total you repay.

/**
 * Calculator maths for costreference.com.
 *
 * Plain functions on `window.CRCALC`. This file is the single source of the maths: it is
 * served to the browser as the calculator bundle and it is the file the Node test
 * (scripts/test-tool-math.mjs) imports. The tests therefore exercise exactly the code users run.
 *
 * Every function validates its inputs and returns NaN rather than throwing, so a bad input
 * produces an error message instead of a broken page.
 */
(function (root) {
  "use strict";

  function isNum(v) { return typeof v === "number" && Number.isFinite(v); }

  /** Standard amortising payment. i = monthly rate, n = months. */
  function monthlyPayment(principal, annualRatePct, months) {
    if (!isNum(principal) || !isNum(annualRatePct) || !isNum(months)) return NaN;
    if (principal  balance) princ = balance;
      if (princ  1e-9) cheaper = a  0
        ? Math.abs(a - b) / Math.max(a, b) * 100 : 0 };
  }

  /** Months to clear a balance at a fixed payment, plus the effect of paying more. */
  function payoffMonths(balance, annualRatePct, payment) {
    if (!isNum(balance) || !isNum(annualRatePct) || !isNum(payment)) return null;
    if (balance

      Loan amount (USD)

      Annual interest rate (%)

      Term (months)

  Calculate payment

The standard amortisation formula is used: payment = P &times; i / (1 &minus;
(1 + i)&minus;n), where i is the monthly rate (annual rate &divide; 12
&divide; 100) and n is the number of months. A zero rate is handled as simple division.
This is the same formula a lender uses to quote a fixed-rate instalment loan; it does not
include origination fees, insurance or any other charge a particular lender may add.

(function () {
  var C = window.CRCALC;
  function $(id) { return document.getElementById(id); }
  function num(id) {
    var el = $(id);
    if (!el) return NaN;
    var v = el.value === "" ? NaN : Number(el.value);
    return Number.isFinite(v) ? v : NaN;
  }
  function money(n) {
    return "$" + n.toLocaleString("en-US", { minimumFractionDigits: 2, maximumFractionDigits: 2 });
  }
  function render() {
    var amount = num("lp-amount"), rate = num("lp-rate"), months = num("lp-months");
    var err = $("lp-err"), out = $("lp-out");
    err.hidden = true;
    if (!Number.isFinite(amount) || amount  100) { err.hidden = false; err.textContent = "Enter an annual rate between 0 and 100."; out.innerHTML = ""; return; }
    if (!Number.isFinite(months) || months  600) { err.hidden = false; err.textContent = "Enter a term between 1 and 600 months."; out.innerHTML = ""; return; }
    var s = C.amortise(amount, rate, months);
    if (!s) { err.hidden = false; err.textContent = "Those inputs cannot be calculated."; out.innerHTML = ""; return; }
    var rows = s.years.map(function (y) {
      return "" + y.month + "" + money(y.balance) + "" + money(y.interest) + "

";
    }).join("");
    out.innerHTML = "" + money(s.payment) + " per month

" +
      "Total interest over " + months + " months: " + money(s.interest) + ". Total repaid: " + money(s.total) + ".

" +
      "Balance and cumulative interest at each year end" +
      "MonthBalanceInterest paid to date

" +
      "" + rows + "

";
  }
  window.__crRender = render;
  $("lp-go").addEventListener("click", render);
  ["lp-amount", "lp-rate", "lp-months"].forEach(function (id) { $(id).addEventListener("input", render); });
  render();
})();

Source: https://costreference.com/tools/loan-payment-calculator/
