You're Invited:Meet the Socket Team at RSAC and BSidesSF 2026, March 23–26.RSVP
Socket
Book a DemoSign in
Socket

bigdecimal.js

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bigdecimal.js

A BigDecimal implementation with native BigInts

Source
npmnpm
Version
1.1.3
Version published
Weekly downloads
1.7K
-36.75%
Maintainers
1
Weekly downloads
 
Created
Source

BigDecimal.js

NPM Version NPM Downloads codecov

BigInt based BigDecimal implementation for Node.js 10.4 and above. This implementation is inspired from java BigDecimal class. This implementation is faster than popular big decimal libraries for most operations. See benchmarks results part below for comparison of each operation.

Advantages of this library

  • Faster than other BigDecimal libraries because of native BigInt
  • Simple API that is almost same with Java's BigDecimal
  • No dependencies
  • Well tested
  • Includes type definition file

Installation

npm install bigdecimal.js

Usage

  • The example usage is given below:
// Single unified constructor for multiple values
const { Big } = require('bigdecimal.js');

// Construct from a string and clone it
const x = Big('1.1111111111111111111111');
const y = new Big(x); // you can also use 'new'

const z = x.add(y);
console.log(z.toString()); // 2.2222222222222222222222

// You can also construct from a number or BigInt:
const u = Big(1.1);
const v = Big(2n);

console.log(u.toString()); // 1.1
console.log(v.toString()); // 2

You can use MathContext to set precision and rounding mode for a specific operation:

const { Big, MC, RoundingMode } = require('bigdecimal.js');

const x = Big('1');
const y = Big('3');

// MC is MathContext constructor that can be used with or without `new`
const res1 = x.divideWithMathContext(y, MC(3)); 
console.log(res1.toString()); // 0.333

const res2 = x.divideWithMathContext(y, new MC(3, RoundingMode.UP));
console.log(res2.toString()); // 0.334

try {
    x.divide(y);
    // throws since full precision is requested but it is not possible
} catch (e) {
    console.log(e); // RangeError: Non-terminating decimal expansion; no exact representable decimal result.
}

Documentation

Testing

  • Install dependencies: npm i
  • Compile: npm run compile
  • Run tests: npm test

Running Benchmarks

There is a benchmark suite that compares

To run the benchmark run npm install and then npm run benchmark.

Benchmark Results

For now, benchmarked against big.js and bigdecimal.

  • Test Machine:

    • M1 2020 Macbook Air
    • 8 GB Ram
    • MacOS Monterey 12.0.1
  • Update Date: January 7th 2022

  • Library versions used:

    • big.js 6.1.1
    • (this library) bigdecimal.js 1.1.1
    • bigdecimal 0.6.1
  • Each operation is run with fixed set of decimal numbers composed of both simple and complex numbers.

  • Micro benchmark framework used is benchmark. Check out benchmarks folder for source code of benchmarks.

  • For now, benchmarked the following operations, all operations will be added soon.

  • Operations per second(op/s):

OperationBigdecimal.jsBig.jsGWTWinner
Add79,13018,67585.21BigDecimal.js
Multiply495,89433,5342,607BigDecimal.js
Subtract72,75418,19088.47BigDecimal.js
Divide15,1231,122658BigDecimal.js
Abs751,2321,405,72213,732Big.js
Compare529,0091,152,9951,010,368Big.js
Remainder9,7053,8782,487BigDecimal.js

Keywords

bigdecimal

FAQs

Package last updated on 29 Jan 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts