decimal.js

An arbitrary-precision Decimal type for JavaScript with full trigonometric support

Library
npm
v10.6.0
7,269 stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
50 /100 Fair
Development Activity 36
Maintenance 8
Community 56
Maturity 60
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
74 /100 Good
Architecture 78
Code Quality 82
Innovation 65
Learning Curve 70

decimal.js provides an arbitrary-precision Decimal type for JavaScript, solving the well-known problem where native floating-point numbers produce results like 0.1 + 0.2 !== 0.3. It replicates most of the methods found on JavaScript’s Number.prototype and Math objects, but computes them to a configurable number of significant digits with predictable rounding, similar in spirit to Python’s decimal module.

Unlike its sibling libraries bignumber.js and big.js (also maintained by the same author), decimal.js additionally implements trigonometric and other advanced functions and supports non-integer powers, making it the largest and most feature-complete of the three — at the cost of a bigger bundle. It has zero dependencies, supports hex/binary/octal input, ships TypeScript declarations, and is used internally by math.js.

What You Get

  • An arbitrary-precision Decimal type supporting integers and floats with immutable, chainable methods
  • Full trigonometric and transcendental function support (sin, cos, atan, ln, exp, etc.) beyond basic arithmetic
  • Configurable precision and rounding per independently-clonable Decimal constructor via Decimal.clone({...})
  • Hexadecimal, binary, and octal number parsing and formatting alongside standard decimal
  • TypeScript declaration files and both CommonJS (decimal.js) and ESM (decimal.mjs) builds

Common Use Cases

  • Financial and monetary calculations where floating-point rounding errors are unacceptable
  • Scientific or engineering computations requiring more significant digits than JavaScript’s native Number can represent
  • Cryptography, cryptocurrency, or blockchain applications needing exact big-integer/big-decimal arithmetic
  • Powering higher-level math libraries (e.g. math.js uses decimal.js internally) that need a reliable arbitrary-precision numeric core

Under The Hood

Architecture - The library is a single ~4,950-line file (decimal.js, mirrored as decimal.mjs for ESM) implementing the Decimal constructor and its full method set. Internally each value is stored as a sign (s), an array of base-10,000,000 digit groups (d), and a base-10 exponent (e) — a classic floating-point-of-digits representation that avoids native float imprecision entirely. Every arithmetic method (plus, times, sqrt, trigonometric functions) operates on these digit arrays and rounds the result according to the constructor’s configured precision/rounding settings before returning a new immutable Decimal.

Tech Stack - Pure JavaScript (99.8%) written to run on ECMAScript 3 for maximum backward compatibility, with zero runtime dependencies. It ships prebuilt CommonJS, ESM, and global-script builds plus a .d.ts/.global.d.ts pair of TypeScript declarations, and includes example minification recipes for uglify-js and terser.

Code Quality - The test/modules/ directory contains 61 separate test modules, one per method (e.g. toFraction, sqrt, pow), runnable individually via node test/modules/<name> or all together via npm test, plus a browser test harness (test/test.html). Given the mathematical nature of the code, correctness is verified through large tables of expected input/output pairs per operation rather than typical unit-test mocking patterns.

API Design - The API deliberately mirrors JavaScript’s native Number.prototype/Math naming (toFixed, toPrecision, sqrt) so developers already familiar with JS numbers have minimal new vocabulary to learn, while method chaining (x.dividedBy(y).plus(z).times(9).floor()) keeps multi-step calculations readable. Most methods have short aliases (div/dividedBy, cmp/comparedTo), and the ability to create independently-configured constructors via Decimal.clone() is a thoughtful touch for libraries embedding decimal.js without polluting global config.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers