decimal.js
An arbitrary-precision Decimal type for JavaScript with full trigonometric support
Repository Health
Technical Analysis
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
Decimaltype supporting integers and floats with immutable, chainable methods - Full trigonometric and transcendental function support (sin, cos, atan, ln, exp, etc.) beyond basic arithmetic
- Configurable
precisionandroundingper independently-clonableDecimalconstructor viaDecimal.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.
Used by 12 apps in this directory
OpenCode
AI Code Assistants
A fully open-source AI coding agent built for the terminal, with a TUI, desktop app, web client, plugin system, and SDK — one of the most-starred AI coding agents on GitHub.
Tianji
Analytics · Monitoring
Replace Google Analytics, UptimeKuma, and Prometheus with one self-hosted platform that tracks websites, monitors uptime, and reports server health.
Trigger.dev
AI Development · Automation · Developer Tools
Build and deploy fully-managed AI agents and background jobs in TypeScript — with no timeouts, durable retries, real-time observability, and elastic scaling built in.