Skip to content

Why a-calc

In One Sentence

a-calc is the most powerful JavaScript precision arithmetic and number formatting library on the planet.

Not one of the best. The best.

The Precision Problem: JavaScript's Original Sin

javascript
// Native JavaScript
0.1 + 0.2; // 0.30000000000000004
0.3 - 0.1; // 0.19999999999999998
0.1 * 0.2; // 0.020000000000000004
0.3 / 0.1; // 2.9999999999999996

In finance, e-commerce, and data reporting, this is fatal.

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

calc("0.1 + 0.2"); // '0.3'
calc("0.3 - 0.1"); // '0.2'
calc("0.1 * 0.2"); // '0.02'
calc("0.3 / 0.1"); // '3'

Performance: 10x Faster

Library500k calculationsRelative
a-calc3.6s10x
mathjs v135.7s1.6x
mathjs v1236s1x (baseline)

This isn't an optimization — it's a generation gap.

Features: One Library Does It All

Featurea-calcmathjsnumeraldecimal.js
Precision arithmetic
Expression parsing
Thousands formatting
Percentage formatting
International number formats
Compact abbreviations
Unit arithmetic
Unit conversion
Conditional expressions
Chain API
Multiple compute engines
Bundle size20KB150KB18KB12KB

mathjs + numeral + decimal.js = 180KB, and still can't match a-calc's 20KB.

Core Capabilities

1. Calculate + Format in One Step

javascript
// Other libraries: calculate then format separately
const result = mathjs.evaluate("price * quantity");
const formatted = numeral(result).format("0,0.00");

// a-calc: one line
calc("price * quantity | =2,", { price: 1234.5, quantity: 2 });
// '2,469.00'

2. Four Rounding Modes

javascript
calc("1.125 | ~-=2"); // '1.12' - truncate
calc("1.125 | ~+=2"); // '1.13' - ceil
calc("1.125 | ~5=2"); // '1.13' - round half up
calc("1.125 | ~6=2"); // '1.12' - banker's rounding

Banker's rounding is the financial industry standard. Most libraries don't support it.

3. Global Number Formats

javascript
calc("1234567.89 | !t:en"); // '1,234,567.89' English/International
calc("1234567.89 | !t:eu"); // '1.234.567,89' European
calc("1234567.89 | !t:indian"); // '12,34,567.89' Indian
calc("1234567.89 | !t:wan"); // '1,2345,678.89' Chinese wan-based

4. Smart Compact Abbreviations

javascript
calc("1234567 | !c"); // '1.23M'
calc("12345 | !c:wan"); // '1.23万'
calc("1073741824 | !c:storage"); // '1GB'

5. Three Compute Engines

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

// Decimal mode: 50-digit precision, full features
set_config({ _compute_mode: "decimal" });

// BigInt mode: lossless large integers
set_config({ _compute_mode: "bigint" });
calc("12345678901234567890 * 2"); // perfectly accurate

// WASM mode: extreme performance
set_config({ _compute_mode: "wasm" });

6. Chain API

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

// Shopping cart calculation
cadd(99.9, 199, 49.5) // item prices
  .mul(0.9) // 10% discount
  .sub(20) // coupon
  .mul(1.06)(
  // tax
  "=2,",
); // '296.33'

7. Conditional Expressions

javascript
calc('score >= 60 ? "Pass" : "Fail"', { score: 75 }); // 'Pass'

calc(
  `
  score >= 90 ? "A" :
  score >= 80 ? "B" :
  score >= 70 ? "C" :
  score >= 60 ? "D" : "F"
`,
  { score: 85 },
); // 'B'

8. Unit Arithmetic & Conversion

javascript
// Unit arithmetic
calc("100$ + 50$", { _unit: true }); // '150$'

// Unit conversion
calc("100 | !ua:USD", {
  _unit_convert_out: { USD: { CNY: 7.2 } },
});

9. Built-in Math Functions

javascript
calc("sqrt(16)"); // '4'
calc("sin(90)"); // '1' (degrees by default)
calc("pow(2, 10)"); // '1024'
calc("max(1, 5, 3)"); // '5'
calc("round(3.7)"); // '4'
calc("abs(-5)"); // '5'

10. Fallback Values

javascript
// Use a if available, otherwise b
calc("(a | b) + 1", { b: 10 }); // '11'
calc("(a | b) + 1", { a: 5, b: 10 }); // '6'

11. Debug Mode

javascript
calc("a + b * c", { a: 1, b: 2, c: 3, _debug: true });
// Prints full calculation trace to console

Migration: Nearly Zero Cost

javascript
// From mathjs
// Before: mathjs.evaluate('1 + 2 * 3')
calc("1 + 2 * 3");

// From numeral
// Before: numeral(1234567).format('0,0.00')
calc("1234567 | =2,");

// From decimal.js
// Before: new Decimal('0.1').plus('0.2').toString()
calc("0.1 + 0.2");

Summary

Dimensiona-calc
Precision50-digit Decimal / unlimited BigInt / 28-digit WASM
Performance10x faster than mathjs
Bundle size20KB, 7x smaller than mathjs
Formattingthousands, percent, scientific, compact, international
Roundingtruncate, ceil, round-half-up, banker's rounding
Ease of usecalculate + format in one line

a-calc doesn't lead in one dimension — it dominates every dimension.

That's why you choose a-calc.

Released under the MIT License