decimal.js
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
Decimal.js is a precise and efficient JavaScript library used for integer and float computations. The essential functionality of the package includes arithmetic operations, hexadecimal, binary, and octal values handling, as well as implementing several of JavaScript's Number.prototype
and Math
methods. Unlike JavaScript's BigDecimal class, decimal.js focuses on significant digits rather than decimal places and ensures precise results even for non-integer powers. Designed for extensive platform compatibility, this library operates using JavaScript 1.5 (ECMAScript 3) features, minimizing dependencies and providing a light footprint for your projects.
Using decimal.js in your project is straightforward and starts with installing the library. If you are working on a Node.js project, the installation can be done via npm using the command npm install decimal.js
. Once installed, the package can be imported using built-in Node.js require()
method:
const Decimal = require('decimal.js');
Or using ES6 import statements:
import Decimal from 'decimal.js';
The core functionality of decimal.js is accessed through the Decimal
constructor, which can parse numbers, strings, or other Decimal instances. Due to precision considerations, it's often recommended to pass larger values as strings:
let x = new Decimal(123.4567);
let y = new Decimal('123456.7e-3');
let z = new Decimal(x);
console.log(x.equals(y) && y.equals(z) && x.equals(z)); // Outputs: true
Complex calculations can be chained:
let result = new Decimal(10).dividedBy(3).plus(7).times(5).floor();
console.log(result.toString()); // Outputs: '23'
The complete documentation for decimal.js is available on the project’s GitHub page. You can find it here. The documentation contains details about the API, usage examples, and other intricate details about the library, ensuring you have access to all the information needed to seamlessly integrate this library into your projects.