Skip to content

Finance

Financial calculations demand extreme precision. a-calc solves JavaScript's floating-point issues perfectly.

Interest Calculations

Simple Interest

javascript
import { calc } from "a-calc";

// Interest = Principal × Rate × Years
const interest = calc("principal * rate * years | =2", {
  principal: 100000,
  rate: 0.045,
  years: 3,
});
// '13500.00'

const total = calc("principal * (1 + rate * years) | =2,", {
  principal: 100000,
  rate: 0.045,
  years: 3,
});
// '113,500.00'

Compound Interest

javascript
const compound = calc("principal * (1 + rate) ** years | =2,", {
  principal: 100000,
  rate: 0.045,
  years: 3,
});
// '114,116.61'

const profit = calc("principal * ((1 + rate) ** years - 1) | =2,", {
  principal: 100000,
  rate: 0.045,
  years: 3,
});
// '14,116.61'

Currency Conversion

javascript
const usdToCny = calc("usd * rate | =2,", { usd: 1000, rate: 7.2456 });
// '7,245.60'

const amounts = [100, 500, 1000, 5000];
const converted = amounts.map((usd) =>
  calc("usd * rate | =2,", { usd, rate: 7.2456 }),
);
// ['724.56', '3,622.80', '7,245.60', '36,228.00']

Loan Calculation

Equal Payment (Annuity)

javascript
function calcMonthlyPayment(principal, annualRate, months) {
  const mr = calc("rate / 12", { rate: annualRate });
  return calc(
    `
    principal * mr * (1 + mr) ** months /
    ((1 + mr) ** months - 1) | ~5=2
  `,
    { principal, mr, months },
  );
}

// $1M loan, 4.9% annual, 30 years
calcMonthlyPayment(1000000, 0.049, 360); // '5307.27'

Banker's Rounding

Financial calculations should use ~6 (banker's rounding):

javascript
calc("1.125 | ~6=2"); // '1.12' (5 preceded by even 2 → round down)
calc("1.135 | ~6=2"); // '1.14' (5 preceded by odd 3 → round up)
calc("1.145 | ~6=2"); // '1.14' (5 preceded by even 4 → round down)
calc("1.155 | ~6=2"); // '1.16' (5 preceded by odd 5 → round up)

Compact Format for Large Numbers

javascript
calc("1234567 | !c"); // '1.23M'
calc("1234567890 | !c"); // '1.23B'
calc("1234567 | +!c"); // '+1.23M'
calc("-1234567 | +!c"); // '-1.23M'

Currency Symbol Formatting

javascript
// !um:unit — symbol between sign and number
calc("1234.56 | + !um:$"); // '+$1234.56'
calc("-567.89 | + !um:$"); // '-$567.89'
calc("12345.6 | +=2, !um:$"); // '+$12,345.60'

// !ua:unit — unit after number
calc("100 | !ua:元"); // '100元'

// !ub:unit — unit before number
calc("100 | !ub:$"); // '$100'

Income Tax

javascript
function calcIncomeTax(income) {
  const taxable = calc("income - 5000", { income });
  if (calc(`${taxable} <= 0 | !n`)) return "0.00";

  const brackets = [
    { limit: 3000, rate: 0.03, deduction: 0 },
    { limit: 12000, rate: 0.1, deduction: 210 },
    { limit: 25000, rate: 0.2, deduction: 1410 },
    { limit: Infinity, rate: 0.45, deduction: 15160 },
  ];

  const bracket = brackets.find((b) => calc(`${taxable} <= ${b.limit} | !n`));
  return calc("taxable * rate - deduction | =2", {
    taxable,
    rate: bracket.rate,
    deduction: bracket.deduction,
  });
}

calcIncomeTax(10000); // '150.00'
calcIncomeTax(20000); // '1590.00'

Released under the MIT License