or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-big-js

A small, fast, easy-to-use library for arbitrary-precision decimal arithmetic

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/big.js@6.2.x

To install, run

npx @tessl/cli install tessl/npm-big-js@6.2.0

index.mddocs/

Big.js

Big.js is a small, fast JavaScript library for arbitrary-precision decimal arithmetic that overcomes JavaScript's floating-point precision limitations. It provides immutable number objects with all standard arithmetic operations and supports configuration for rounding behavior and precision.

Package Information

  • Package Name: big.js
  • Package Type: npm
  • Language: JavaScript (ES3 compatible)
  • Installation: npm install big.js

Core Imports

ES Module:

import Big from "big.js";

CommonJS:

const Big = require("big.js");

Browser (global):

<script src="big.js"></script>
<!-- Big is now available globally -->

Basic Usage

import Big from "big.js";

// Create Big numbers from numbers, strings, or other Big numbers
const x = new Big(123.4567);
const y = Big('123456.7e-3');  // 'new' is optional
const z = new Big(x);

// All represent the same value
console.log(x.eq(y) && x.eq(z) && y.eq(z)); // true

// Arithmetic operations return new Big instances
const sum = x.plus(y);
const difference = x.minus(y);
const product = x.times(2);
const quotient = x.div(2);

// Method chaining is supported
const result = x.div(y).plus(z).times(9).minus('1.234567801234567e+8');

// String conversion with various formats
console.log(x.toString());           // "123.4567"
console.log(x.toFixed(2));           // "123.46"
console.log(x.toExponential(3));     // "1.235e+2"

Architecture

Big.js stores numbers in decimal floating point format with three components:

  • Coefficient (c): Array of digits representing the significant digits
  • Exponent (e): Position of the decimal point
  • Sign (s): 1 for positive, -1 for negative

This internal structure is accessible for advanced usage but should be treated as read-only.

Capabilities

Number Creation

Create Big number instances from various input types.

/**
 * Creates a new Big number instance, or returns a new Big constructor when called without arguments
 * @param n - A numeric value: number, string, or Big instance (optional)
 * @returns Big number instance, or new Big constructor function if n is undefined
 */
function Big(n?: number | string | Big): Big;

Configuration Properties

Global configuration properties that affect rounding behavior and precision.

/** Maximum decimal places for division operations (default: 20, range: 0-1000000) */
Big.DP: number;

/** Rounding mode for division operations (default: 1, range: 0-3) */
Big.RM: number;

/** Negative exponent threshold for exponential notation in toString (default: -7) */
Big.NE: number;

/** Positive exponent threshold for exponential notation in toString (default: 21) */
Big.PE: number;

/** Strict mode - throws errors for primitive number inputs (default: false) */
Big.strict: boolean;

/** Rounding mode constants */
Big.roundDown: 0;      // Toward zero (truncate)
Big.roundHalfUp: 1;    // To nearest neighbor, ties away from zero
Big.roundHalfEven: 2;  // To nearest neighbor, ties to even
Big.roundUp: 3;        // Away from zero

Arithmetic Operations

Core mathematical operations that return new Big instances.

/**
 * Returns the absolute value of this Big number
 * @returns New Big with absolute value
 */
abs(): Big;

/**
 * Addition - returns the sum of this Big and y
 * @param y - Number to add (number, string, or Big)
 * @returns New Big with sum
 */
plus(y: number | string | Big): Big;
add(y: number | string | Big): Big; // Alias for plus

/**
 * Subtraction - returns the difference of this Big and y
 * @param y - Number to subtract (number, string, or Big)
 * @returns New Big with difference
 */
minus(y: number | string | Big): Big;
sub(y: number | string | Big): Big; // Alias for minus

/**
 * Multiplication - returns the product of this Big and y
 * @param y - Number to multiply (number, string, or Big)
 * @returns New Big with product
 */
times(y: number | string | Big): Big;
mul(y: number | string | Big): Big; // Alias for times

/**
 * Division - returns quotient of this Big divided by y, rounded to Big.DP decimal places
 * @param y - Divisor (number, string, or Big)
 * @returns New Big with quotient
 * @throws Error if y is zero
 */
div(y: number | string | Big): Big;

/**
 * Modulo - returns remainder of this Big divided by y
 * @param y - Divisor (number, string, or Big)
 * @returns New Big with remainder
 * @throws Error if y is zero
 */
mod(y: number | string | Big): Big;

/**
 * Exponentiation - returns this Big raised to power n
 * @param n - Integer exponent (-1000000 to 1000000)
 * @returns New Big with result
 * @throws Error if n is not integer or out of range
 */
pow(n: number): Big;

/**
 * Square root - returns square root of this Big, rounded to Big.DP decimal places
 * @returns New Big with square root
 * @throws Error if this Big is negative
 */
sqrt(): Big;

/**
 * Negation - returns negated value of this Big
 * @returns New Big with opposite sign
 */
neg(): Big;

Comparison Operations

Methods for comparing Big numbers, returning boolean results.

/**
 * Compare this Big with y
 * @param y - Number to compare (number, string, or Big)
 * @returns 1 if greater, 0 if equal, -1 if less
 */
cmp(y: number | string | Big): number;

/**
 * Equality comparison
 * @param y - Number to compare (number, string, or Big)
 * @returns true if equal
 */
eq(y: number | string | Big): boolean;

/**
 * Greater than comparison
 * @param y - Number to compare (number, string, or Big)
 * @returns true if this Big is greater than y
 */
gt(y: number | string | Big): boolean;

/**
 * Greater than or equal comparison
 * @param y - Number to compare (number, string, or Big)
 * @returns true if this Big is greater than or equal to y
 */
gte(y: number | string | Big): boolean;

/**
 * Less than comparison
 * @param y - Number to compare (number, string, or Big)
 * @returns true if this Big is less than y
 */
lt(y: number | string | Big): boolean;

/**
 * Less than or equal comparison
 * @param y - Number to compare (number, string, or Big)
 * @returns true if this Big is less than or equal to y
 */
lte(y: number | string | Big): boolean;

Rounding and Precision

Methods for controlling precision and rounding behavior.

/**
 * Round to specified decimal places
 * @param dp - Decimal places (optional, default 0, range: -1000000 to 1000000)
 * @param rm - Rounding mode (optional, uses Big.RM if not specified)
 * @returns New Big rounded to dp decimal places
 */
round(dp?: number, rm?: number): Big;

/**
 * Round to specified significant digits
 * @param sd - Significant digits (1 to 1000000)
 * @param rm - Rounding mode (optional, uses Big.RM if not specified)
 * @returns New Big rounded to sd significant digits
 */
prec(sd: number, rm?: number): Big;

String Conversion

Methods for converting Big numbers to various string representations.

/**
 * Standard string representation
 * Uses exponential notation based on Big.NE and Big.PE thresholds
 * @returns String representation
 */
toString(): string;

/**
 * JSON serialization (same as toString)
 * Also provides Node.js util.inspect custom formatting
 * @returns String representation for JSON.stringify and console output
 */
toJSON(): string;

/**
 * Fixed-point notation with specified decimal places
 * @param dp - Decimal places (optional, range: 0-1000000)
 * @param rm - Rounding mode (optional, uses Big.RM if not specified)
 * @returns Fixed-point string
 */
toFixed(dp?: number, rm?: number): string;

/**
 * Exponential notation with specified decimal places
 * @param dp - Decimal places (optional, range: 0-1000000)
 * @param rm - Rounding mode (optional, uses Big.RM if not specified)
 * @returns Exponential notation string
 */
toExponential(dp?: number, rm?: number): string;

/**
 * String with specified significant digits
 * Uses exponential notation if needed based on magnitude
 * @param sd - Significant digits (optional, range: 1-1000000)
 * @param rm - Rounding mode (optional, uses Big.RM if not specified)
 * @returns String with specified precision
 */
toPrecision(sd?: number, rm?: number): string;

/**
 * Primitive value conversion for type coercion
 * @returns String representation
 * @throws Error if Big.strict is true
 */
valueOf(): string;

/**
 * Convert to primitive number
 * @returns JavaScript number (may lose precision)
 * @throws Error if Big.strict is true and conversion is imprecise
 */
toNumber(): number;

Instance Properties

Properties accessible on Big instances (read-only for normal usage).

/** Coefficient array containing the significant digits */
c: number[];

/** Exponent indicating decimal point position */
e: number;

/** Sign: 1 for positive, -1 for negative */
s: number;

/** Reference to the Big constructor */
constructor: typeof Big;

Types

/**
 * Big number class for arbitrary-precision decimal arithmetic
 */
declare class Big {
  /** Coefficient array */
  c: number[];
  /** Exponent */
  e: number;
  /** Sign */
  s: number;
  /** Constructor reference */
  constructor: typeof Big;
  
  constructor(n?: number | string | Big);
  
  // Static configuration properties
  static DP: number;
  static RM: number;
  static NE: number;
  static PE: number;
  static strict: boolean;
  
  // Static rounding constants
  static roundDown: 0;
  static roundHalfUp: 1;
  static roundHalfEven: 2;
  static roundUp: 3;
  
  // Instance methods (see Capabilities sections above for details)
  abs(): Big;
  plus(y: number | string | Big): Big;
  add(y: number | string | Big): Big;
  minus(y: number | string | Big): Big;
  sub(y: number | string | Big): Big;
  times(y: number | string | Big): Big;
  mul(y: number | string | Big): Big;
  div(y: number | string | Big): Big;
  mod(y: number | string | Big): Big;
  pow(n: number): Big;
  sqrt(): Big;
  neg(): Big;
  cmp(y: number | string | Big): number;
  eq(y: number | string | Big): boolean;
  gt(y: number | string | Big): boolean;
  gte(y: number | string | Big): boolean;
  lt(y: number | string | Big): boolean;
  lte(y: number | string | Big): boolean;
  round(dp?: number, rm?: number): Big;
  prec(sd: number, rm?: number): Big;
  toString(): string;
  toJSON(): string;
  toFixed(dp?: number, rm?: number): string;
  toExponential(dp?: number, rm?: number): string;
  toPrecision(sd?: number, rm?: number): string;
  valueOf(): string;
  toNumber(): number;
}

declare const Big: {
  new (n?: number | string | Big): Big;
  (n?: number | string | Big): Big | (() => typeof Big);
  DP: number;
  RM: number;
  NE: number;
  PE: number;
  strict: boolean;
  roundDown: 0;
  roundHalfUp: 1;
  roundHalfEven: 2;
  roundUp: 3;
};

export default Big;

Error Handling

Big.js throws Error objects with descriptive messages in the following scenarios:

  • Invalid number format: When constructor receives unparseable string
  • Invalid decimal places: When dp parameter is not integer or out of range (0-1000000)
  • Invalid rounding mode: When rm parameter is not 0, 1, 2, or 3
  • Division by zero: When divisor is zero in div() or mod() operations
  • Invalid precision: When sd parameter is not integer or out of range (1-1000000)
  • Invalid exponent: When pow() exponent is not integer or out of range (-1000000 to 1000000)
  • Square root of negative: When sqrt() is called on negative number
  • Strict mode violations: When Big.strict is true and:
    • Constructor receives primitive number (instead of string)
    • valueOf() is called
    • toNumber() would lose precision

Usage Examples

Configuration:

// Configure global precision and rounding
Big.DP = 10;                    // 10 decimal places for division
Big.RM = Big.roundHalfUp;       // Round ties away from zero

// Enable strict mode
Big.strict = true;

Precision Arithmetic:

// JavaScript floating point issues
console.log(0.1 + 0.2);         // 0.30000000000000004

// Big.js precision
const a = new Big('0.1');
const b = new Big('0.2');
console.log(a.plus(b).toString()); // "0.3"

Financial Calculations:

const price = new Big('19.99');
const tax = new Big('0.08');
const quantity = new Big('3');

const subtotal = price.times(quantity);
const taxAmount = subtotal.times(tax);
const total = subtotal.plus(taxAmount);

console.log(total.toFixed(2));   // "64.77"

Advanced Usage with Multiple Constructors:

// Create independent Big constructors with different configurations
const FinancialBig = Big();      // New constructor
FinancialBig.DP = 2;             // 2 decimal places for currency
FinancialBig.RM = FinancialBig.roundHalfUp;

const ScientificBig = Big();     // Another constructor  
ScientificBig.DP = 50;           // High precision for science
ScientificBig.RM = ScientificBig.roundHalfEven;