decimal.js is an arbitrary-precision Decimal type for JavaScript, providing robust arithmetic capabilities for numbers that require exact precision, such as financial calculations. Currently at version 10.6.0, it offers a stable and actively maintained solution. Unlike bignumber.js, decimal.js defines precision in terms of significant digits rather than decimal places, aligning with Python's decimal module. It further distinguishes itself by including comprehensive trigonometric functions and support for non-integer powers, making it a more feature-rich (and larger) library than bignumber.js or big.js. The library has no external dependencies and maintains wide platform compatibility by using only ECMAScript 3 features. It ships with TypeScript declaration files, ensuring type safety for modern JavaScript projects.
npm install decimal.jsVerified import paths — ran on the pinned version, not inferred.
Illustrates initializing Decimal instances from various types and highlights common precision pitfalls with native JavaScript numbers versus Decimal's exact arithmetic.
Always pass numerical values as strings when they exceed JavaScript's native Number precision or range, especially for financial or scientific calculations. E.g., `new Decimal('1.0000000000000001')` instead of `new Decimal(1.0000000000000001)`.Ensure all parts of a calculation are handled by Decimal.js instances from the start. Convert all initial numbers to Decimal instances (preferably from strings) before performing any arithmetic operations. E.g., `new Decimal('0.1').plus(new Decimal('0.2'))`.Familiarize yourself with the library's precision settings (e.g., `Decimal.set({ precision: N })`) and rounding modes (`Decimal.set({ rounding: Decimal.ROUND_HALF_UP })`) to ensure calculations meet specific requirements. Understand that intermediate results are rounded according to the global precision setting.Always instantiate `Decimal` with `new`, e.g., `const num = new Decimal('123.45');`. For CommonJS, ensure `const Decimal = require('decimal.js');` and not `const { Decimal } = require('decimal.js');`.Convert all numeric inputs to strings before creating Decimal instances, especially for values prone to floating-point issues (e.g., `new Decimal('0.1').plus(new Decimal('0.2'))`). Ensure all arithmetic operations are performed using Decimal methods.No dependency data recorded yet.