decimal.js
Arbitrary-precision Decimal type for JavaScript with configurable precision, rounding, and a broad math API.
Official Documentation ↗v10. Check the official docs for the latest API surface.Installation
npm install decimal.jspnpm add decimal.jsMethods
No methods match this filter.
Overview
decimal.js provides an arbitrary-precision Decimal type with configurable precision and rounding plus a broad math API. This reference lists the most frequently used APIs with accurate signatures and runnable snippets.
Core APIs
new Decimal— Creates an arbitrary-precision decimal value from a string or number.times— Multiplies two decimals exactly, subject to the configured precision.toSignificantDigits— Rounds the value to a number of significant digits with an explicit rounding mode.
Example
const Decimal = require('decimal.js');
const d = new Decimal('1.005');
console.log(d.toFixed(2));
Notes
Division and transcendental functions depend on the global precision setting, so configure it before computing and keep it consistent across a service.
Verification
Multiply 19.99 by 3 and confirm the exact string is 59.97 with no floating-point tail.
Errors
The following points are quoted verbatim from the official documentation. 来源:https://mikemcl.github.io/decimal.js/ · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:49:11.660Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.
- Throws on an invalid object or configuration property value. (https://mikemcl.github.io/decimal.js/)
- If a result is incorrectly rounded the maximum error will be 1 ulp (unit in the last place). (https://mikemcl.github.io/decimal.js/)
FAQ
- How is decimal.js different from bignumber.js?
- Both are arbitrary-precision decimal libraries; decimal.js exposes a Decimal class with its own math API and configuration surface.
- How do I set the precision?
- Call Decimal.set({ precision }) to control significant digits used by operations such as division.
- Does it support trigonometry?
- Yes. Decimal provides sin, cos, and other functions at the configured precision, though they can be slower than native Math calls.
Report a problem with this API docOpens a pre-filled issue on GitHub.