Introduction
@wzo/calc is a JavaScript precision math + number formatting library.
@wzo/calc
@wzo/calc is a precision math + number formatting library with expression evaluation support and zero runtime dependencies.
Why this library
JavaScript's native floating-point arithmetic has precision issues:
0.1 + 0.2 // 0.30000000000000004
0.3 - 0.1 // 0.19999999999999998
0.1 * 0.2 // 0.020000000000000004
Number(1.005).toFixed(2) // "1.00" (should be "1.01")
@wzo/calc produces exact decimal results:
calc('0.1 + 0.2') // "0.3"
calc('0.3 - 0.1') // "0.2"
calc('0.1 * 0.2') // "0.02"
calc('1.005', { _fmt: { decimals: 2, rounding: 'round' } }) // "1.01"
Core features
- Precision arithmetic — internally represents numbers as "digits × 10^(-exp)" using BigInt; all rational operations are strictly correct
- Rich formatting — thousands separators, percent, compact notation, fractions, scientific notation, European / Indian styles, 6 rounding strategies, type-friendly object-based configuration
- Chaining API —
chainAdd(10).sub(3).mul(2)() - Aggregation —
calcSum / calcAvg / calcMedian / calcMax / calcMin - Zero runtime dependencies — ~12KB gzip, works in browser and Node
- Full TypeScript — type inference included