DecimalX is a high-precision decimal arithmetic library for JavaScript and TypeScript, providing an easy-to-use API for performing arithmetic operations on decimal numbers.
This package is designed to handle calculations with a large number of decimal places, ensuring accuracy and performance in financial applications, scientific computations, and wherever else precise decimal arithmetic is required.
To get started with DecimalX, install the package using npm:
npm install decimalx
import { DecimalFactory } from 'decimalx';
// precision is total number of numbers allowed
// scale is the portion of the total that should be right of the decimal point
let Decimal = DecimalFactory({ precision: 20, scale: 2})
let decimalA = new Decimal('10.6');
let decimalB = new Decimal('2.3');
let sum = decimalA.add(decimalB);
let difference = decimalA.subtract(decimalB);
let product = decimalA.mul(decimalB);
let distribution = decimalA.distribute(3);
console.log(sum.valueAsString()); // '12.90'
console.log(difference.valueAsString()); // '8.30'
console.log(product.valueAsString()); // '24.38'
console.log(distribution.map(d => d.valueAsString())); // ['3.50', '3.50', '3.60']
valueAsBigint()
- Returns the value as a BigIntvalueAsNumber()
- Returns the value as a numbervalueAsString()
- Returns the value as a stringdefinition()
- Returns the precision and scale used for the Decimal