CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-bignumber-js

A JavaScript library for arbitrary-precision decimal and non-decimal arithmetic

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

comparison.mddocs/

Comparison Methods

Comprehensive comparison functionality for equality testing, ordering, and special value detection with precise decimal comparison.

Capabilities

Equality Comparison

Tests if two BigNumbers have equal values with exact precision.

/**
 * Returns true if this BigNumber equals n, otherwise false
 * @param n - Value to compare
 * @param base - Base of n (2-36, optional)
 * @returns Boolean indicating equality
 */
isEqualTo(n: BigNumber.Value, base?: number): boolean;

// Alias
eq(n: BigNumber.Value, base?: number): boolean;

Usage Examples:

import BigNumber from "bignumber.js";

const x = new BigNumber(0);
x.isEqualTo('1e-324');          // false (precise comparison)
x.eq('1e-324');                 // false (alias)

// JavaScript comparison issues
console.log(0 === 1e-324);      // true (imprecise)

// Proper equality checks
BigNumber(-0).isEqualTo(0);     // true (0 === -0)
BigNumber(255).eq('ff', 16);    // true (255 === 255)

// NaN handling
const nan = new BigNumber(NaN);
nan.isEqualTo(NaN);             // false (NaN !== NaN)

Greater Than Comparison

Tests if this BigNumber is greater than another value.

/**
 * Returns true if this BigNumber is greater than n
 * @param n - Value to compare
 * @param base - Base of n (2-36, optional)
 * @returns Boolean indicating if this > n
 */
isGreaterThan(n: BigNumber.Value, base?: number): boolean;

// Alias
gt(n: BigNumber.Value, base?: number): boolean;

Usage Examples:

const x = new BigNumber(0.1);
const y = BigNumber(0.3).minus(0.2);

// Precise comparison
x.isGreaterThan(y);             // false (0.1 is not > 0.1)
x.gt(y);                        // false (alias)

// JavaScript floating-point issues
console.log(0.1 > (0.3 - 0.2)); // true (due to precision errors)

// Base conversion
BigNumber(11, 3).gt(11.1, 2);  // true

Less Than Comparison

Tests if this BigNumber is less than another value.

/**
 * Returns true if this BigNumber is less than n
 * @param n - Value to compare
 * @param base - Base of n (2-36, optional)
 * @returns Boolean indicating if this < n
 */
isLessThan(n: BigNumber.Value, base?: number): boolean;

// Alias
lt(n: BigNumber.Value, base?: number): boolean;

Usage Examples:

const x = new BigNumber(0.3).minus(0.2);
x.isLessThan(0.1);              // false (0.1 is not < 0.1)
x.lt(0.1);                      // false (alias)

// JavaScript comparison
console.log((0.3 - 0.2) < 0.1); // true (imprecise)

BigNumber(0).lt(x);             // true
BigNumber(11.1, 2).lt(11, 3);   // true

Greater Than or Equal Comparison

Tests if this BigNumber is greater than or equal to another value.

/**
 * Returns true if this BigNumber is greater than or equal to n
 * @param n - Value to compare
 * @param base - Base of n (2-36, optional)
 * @returns Boolean indicating if this >= n
 */
isGreaterThanOrEqualTo(n: BigNumber.Value, base?: number): boolean;

// Alias
gte(n: BigNumber.Value, base?: number): boolean;

Usage Examples:

const x = new BigNumber(0.3).minus(0.2);
x.isGreaterThanOrEqualTo(0.1);  // true (0.1 >= 0.1)
x.gte(0.1);                     // true (alias)

// JavaScript comparison
console.log((0.3 - 0.2) >= 0.1); // false (imprecise)

BigNumber(1).gte(x);            // true
BigNumber(10, 18).gte('i', 36); // true

Less Than or Equal Comparison

Tests if this BigNumber is less than or equal to another value.

/**
 * Returns true if this BigNumber is less than or equal to n
 * @param n - Value to compare
 * @param base - Base of n (2-36, optional)
 * @returns Boolean indicating if this <= n
 */
isLessThanOrEqualTo(n: BigNumber.Value, base?: number): boolean;

// Alias
lte(n: BigNumber.Value, base?: number): boolean;

Usage Examples:

const x = new BigNumber(0.1);
x.isLessThanOrEqualTo(BigNumber(0.3).minus(0.2)); // true
x.lte(BigNumber(0.3).minus(0.2));                 // true (alias)

// JavaScript comparison
console.log(0.1 <= (0.3 - 0.2));                 // false (imprecise)

BigNumber(-1).lte(x);           // true
BigNumber(10, 18).lte('i', 36); // true

General Comparison

Returns numeric comparison result similar to Array.sort() compareFn.

/**
 * Compares this BigNumber with n
 * @param n - Value to compare
 * @param base - Base of n (2-36, optional)
 * @returns 1 if this > n, -1 if this < n, 0 if equal, null if NaN
 */
comparedTo(n: BigNumber.Value, base?: number): 1 | -1 | 0 | null;

Usage Examples:

const x = new BigNumber(Infinity);
const y = new BigNumber(5);

x.comparedTo(y);                // 1 (Infinity > 5)
x.comparedTo(x.minus(1));       // 0 (Infinity === Infinity)
y.comparedTo(NaN);              // null (comparison with NaN)
y.comparedTo('110', 2);         // -1 (5 < 6 in decimal)

// Useful for sorting
const numbers = [
  new BigNumber('3.14'),
  new BigNumber('2.71'),
  new BigNumber('1.41')
];
numbers.sort((a, b) => a.comparedTo(b));

Special Value Checks

Methods to detect special numeric values and properties.

/**
 * Returns true if this BigNumber is finite (not ±Infinity or NaN)
 */
isFinite(): boolean;

/**
 * Returns true if this BigNumber is an integer
 */
isInteger(): boolean;

/**
 * Returns true if this BigNumber is NaN
 */
isNaN(): boolean;

/**
 * Returns true if this BigNumber is negative (including -0)
 */
isNegative(): boolean;

/**
 * Returns true if this BigNumber is positive (not negative or zero)
 */
isPositive(): boolean;

/**
 * Returns true if this BigNumber is zero (including -0)
 */
isZero(): boolean;

Usage Examples:

// Finite check
const x = new BigNumber(1);
x.isFinite();                   // true

const y = new BigNumber(Infinity);
y.isFinite();                   // false

// Integer check
new BigNumber(1).isInteger();   // true
new BigNumber(123.456).isInteger(); // false

// NaN check
new BigNumber(NaN).isNaN();     // true
new BigNumber(42).isNaN();      // false

// Sign checks
new BigNumber(-0).isNegative(); // true
new BigNumber(2).isNegative();  // false
new BigNumber(2).isPositive();  // true
new BigNumber(-0).isPositive(); // false

// Zero check
new BigNumber(-0).isZero();     // true
new BigNumber(0).isZero();      // true
new BigNumber(0.1).isZero();    // false

Comparison Chain Examples

Comparison methods can be combined for range checks and complex conditions.

Usage Examples:

const x = new BigNumber('42.5');

// Range checks
const inRange = x.gte(0) && x.lte(100);              // true
const positive = x.isPositive();                      // true
const notZero = !x.isZero();                         // true

// Complex conditions
const isValidPrice = x.isFinite() && x.isPositive() && !x.isNaN();

// Sorting array of BigNumbers
const values = [
  new BigNumber('100.1'),
  new BigNumber('10.01'),
  new BigNumber('1.001')
];

values.sort((a, b) => a.comparedTo(b));
// Results in ascending order

// Find maximum value
const max = values.reduce((max, current) => 
  current.gt(max) ? current : max
);

docs

arithmetic.md

comparison.md

configuration.md

constructor.md

conversion.md

index.md

precision.md

tile.json