# Total cost of ownership calculator

Add purchase price, financing, running costs and resale value to get the real annual cost of ownership.

/**
 * Calculator maths for currentcosts.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

      Purchase price (USD)

      Total financing cost over the term (USD)

      Running costs per year (USD)

      Years you keep it

      Resale value at the end (USD)

  Calculate true cost

Total cost of ownership is purchase price plus financing cost plus running costs
across the holding period, minus what you get back when you sell. It is the number to compare
against a cheaper purchase with higher running costs: a lower sticker price frequently loses on a
per-year basis.

(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: 0, maximumFractionDigits: 0 }); }
  function render() {
    var price = num("tc-price"), fin = num("tc-finance"), run = num("tc-running"),
        years = num("tc-years"), resale = num("tc-resale");
    var err = $("tc-err"), out = $("tc-out");
    err.hidden = true;
    if (!Number.isFinite(price) || price " + money(r.perYear) + " per year

" +
      "Total cost across " + years + " years: " + money(r.total) + ", or " + money(r.perMonth) + " per month. Of that, " + money(fin + run * years) + " is money spent and gone (financing and running costs) and " + money(price - resale) + " is the value you consumed by owning it.

" +
      "Cost build-upComponentAmount

" +
      "Purchase price" + money(price) + "

" +
      "Financing cost over the term" + money(fin) + "

" +
      "Running costs (" + money(run) + " x " + years + " years)" + money(run * years) + "

" +
      "Less resale value-" + money(resale) + "

" +
      "Total cost of ownership" + money(r.total) + "

" +
      "Per year" + money(r.perYear) + "

" +
      "Per month" + money(r.perMonth) + "

" +
      "

";
  }
  window.__crRender = render;
  $("tc-go").addEventListener("click", render);
  ["tc-price", "tc-finance", "tc-running", "tc-years", "tc-resale"].forEach(function (id) { $(id).addEventListener("input", render); });
  render();
})();

Source: https://currentcosts.com/tools/total-cost-of-ownership-calculator/
