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
APITable
Low Code Platforms · Databases
API-first collaborative spreadsheet-database platform that auto-generates REST APIs and lets teams build internal tools, CRMs, and dashboards without code.
Amplication
Developer Tools · AI Code Assistants · Automation
Create production-ready backend services with your organization's standards baked in — generating NestJS, Prisma, and GraphQL code that you own and control.
DataEase
Analytics · Data Engineering · AI Assistants
Open-source BI tool with drag-and-drop dashboards, 20+ data source connectors, and AI-powered natural language queries — a self-hosted alternative to Tableau.
Dify
No Code Platforms · AI Development · Developer Tools
Visual LLM workflow platform with RAG pipelines, agent capabilities, and model management for building production AI applications.
GrowthBook
Developer Tools · Analytics · Monitoring
Open source feature flags, A/B testing, and warehouse-native experimentation that queries your existing data infrastructure—no data movement required.
KeystoneJS
CMS · Developer Tools
The superpowered headless CMS for developers built with GraphQL and React
Langfuse
AI Development · Monitoring
Open source AI engineering platform for LLM observability, prompt management, evaluation, and debugging — self-host in minutes or use Langfuse Cloud.
LimeSurvey
Forms Surveys
The world's most flexible open-source survey platform with 900+ templates, conditional logic, 80+ languages, and full GDPR compliance for any scale.
LLM Gateway
AI Development · Devops
One API endpoint for 25+ LLM providers — route, track costs, enforce compliance, and switch models without changing your code.