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

conversion.mddocs/

Conversion and Formatting

Multiple output formats including strings, numbers, fractions, and locale-specific formatting with base conversion support.

Capabilities

String Conversion

Converts BigNumber to string representation with optional base specification.

/**
 * Returns string representation of this BigNumber in specified base
 * @param base - Number base (2-36, optional, defaults to 10)
 * @returns String representation
 */
toString(base?: number): string;

Usage Examples:

import BigNumber from "bignumber.js";

const x = new BigNumber(750000);
x.toString();                   // "750000"

// Base conversion
const y = new BigNumber(362.875);
y.toString(2);                  // "101101010.111"
y.toString(9);                  // "442.77777777777777777778"
y.toString(16);                 // "16a.e"
y.toString(32);                 // "ba.s"

// Exponential notation based on EXPONENTIAL_AT setting
BigNumber.config({ EXPONENTIAL_AT: 5 });
x.toString();                   // "7.5e+5"

// Base conversion with rounding
BigNumber.config({ DECIMAL_PLACES: 4 });
const z = new BigNumber('1.23456789');
z.toString();                   // "1.23456789" (no base = no rounding)
z.toString(10);                 // "1.2346" (base specified = rounded)

Fixed-Point Notation

Converts to fixed-point (non-exponential) notation with specified decimal places.

/**
 * Returns fixed-point notation string
 * @param decimalPlaces - Number of decimal places (0 to 1e9, optional)
 * @param roundingMode - Rounding mode (0-8, optional)
 * @returns String in fixed-point notation
 */
toFixed(decimalPlaces?: number, roundingMode?: BigNumber.RoundingMode): string;

Usage Examples:

const x = new BigNumber(3.456);
x.toFixed();                    // "3.456" (unrounded)
x.toFixed(0);                   // "3"
x.toFixed(2);                   // "3.46"
x.toFixed(2, 1);                // "3.45" (ROUND_DOWN)
x.toFixed(5);                   // "3.45600"

// JavaScript comparison
const jsNum = 3.456;
jsNum.toFixed();                // "3" (JavaScript default)
jsNum.toFixed(2);               // "3.46"

// Always returns normal notation (never exponential)
const big = new BigNumber('1.23e+21');
big.toFixed();                  // "1230000000000000000000"

Exponential Notation

Converts to exponential notation with specified decimal places.

/**
 * Returns exponential notation string
 * @param decimalPlaces - Number of decimal places (0 to 1e9, optional)
 * @param roundingMode - Rounding mode (0-8, optional)
 * @returns String in exponential notation
 */
toExponential(decimalPlaces?: number, roundingMode?: BigNumber.RoundingMode): string;

Usage Examples:

const x = new BigNumber(45.6);
x.toExponential();              // "4.56e+1"
x.toExponential(0);             // "5e+1"
x.toExponential(1);             // "4.6e+1"
x.toExponential(1, 1);          // "4.5e+1" (ROUND_DOWN)
x.toExponential(3);             // "4.560e+1"

// JavaScript comparison
const jsNum = 45.6;
jsNum.toExponential();          // "4.56e+1"
jsNum.toExponential(0);         // "5e+1"

Precision Notation

Converts to string with specified number of significant digits.

/**
 * Returns string with specified precision (significant digits)
 * @param significantDigits - Number of significant digits (1 to 1e9, optional)
 * @param roundingMode - Rounding mode (0-8, optional)
 * @returns String with specified precision
 */
toPrecision(significantDigits?: number, roundingMode?: BigNumber.RoundingMode): string;

Usage Examples:

const x = new BigNumber(45.6);
x.toPrecision();                // "45.6" (same as toString)
x.toPrecision(1);               // "5e+1"
x.toPrecision(2, 0);            // "4.6e+1" (ROUND_UP)
x.toPrecision(2, 1);            // "4.5e+1" (ROUND_DOWN)
x.toPrecision(5);               // "45.600"

// Exponential vs fixed-point based on magnitude
const small = new BigNumber(0.00456);
small.toPrecision(2);           // "0.0046"
small.toPrecision(1);           // "0.005"

Formatted Output

Converts to formatted string with locale-specific separators and grouping.

/**
 * Returns formatted string according to FORMAT settings
 * @param decimalPlaces - Number of decimal places (optional)
 * @param roundingMode - Rounding mode (0-8, optional)
 * @param format - Formatting options (optional)
 * @returns Formatted string
 */
toFormat(decimalPlaces?: number, roundingMode?: BigNumber.RoundingMode, format?: BigNumber.Format): string;
toFormat(decimalPlaces?: number, format?: BigNumber.Format): string;
toFormat(format?: BigNumber.Format): string;

Usage Examples:

// Set global formatting options
BigNumber.config({
  FORMAT: {
    decimalSeparator: '.',
    groupSeparator: ',',
    groupSize: 3,
    secondaryGroupSize: 0,
    fractionGroupSeparator: ' ',
    fractionGroupSize: 0
  }
});

const x = new BigNumber('123456789.123456789');
x.toFormat();                   // "123,456,789.123456789"
x.toFormat(3);                  // "123,456,789.123"

// Custom formatting per call
const customFormat = {
  decimalSeparator: ',',
  groupSeparator: '.',
  groupSize: 3,
  secondaryGroupSize: 2
};

x.toFormat(customFormat);       // "12.34.56.789,123456789"
x.toFormat(2, customFormat);    // "12.34.56.789,12"
x.toFormat(3, BigNumber.ROUND_UP, customFormat); // "12.34.56.789,124"

// Indian numbering system
const indianFormat = {
  groupSeparator: ',',
  groupSize: 3,
  secondaryGroupSize: 2
};
const indian = new BigNumber('12345678');
indian.toFormat(indianFormat);  // "1,23,45,678"

Fraction Conversion

Converts BigNumber to fraction representation with optional maximum denominator.

/**
 * Returns fraction as array of [numerator, denominator] BigNumbers
 * @param maxDenominator - Maximum denominator value (optional)
 * @returns Array containing numerator and denominator BigNumbers
 */
toFraction(maxDenominator?: BigNumber.Value): [BigNumber, BigNumber];

Usage Examples:

const x = new BigNumber(1.75);
x.toFraction();                 // [BigNumber("7"), BigNumber("4")]

const pi = new BigNumber('3.14159265358');
pi.toFraction();                // [BigNumber("157079632679"), BigNumber("50000000000")]
pi.toFraction(100000);          // [BigNumber("312689"), BigNumber("99532")]
pi.toFraction(10000);           // [BigNumber("355"), BigNumber("113")]
pi.toFraction(100);             // [BigNumber("311"), BigNumber("99")]
pi.toFraction(10);              // [BigNumber("22"), BigNumber("7")]
pi.toFraction(1);               // [BigNumber("3"), BigNumber("1")]

// Working with fraction results
const frac = x.toFraction();
const numerator = frac[0];      // BigNumber("7")
const denominator = frac[1];    // BigNumber("4")
console.log(`${numerator}/${denominator}`); // "7/4"

Number Conversion

Converts BigNumber to JavaScript primitive number (may lose precision).

/**
 * Returns JavaScript primitive number (precision may be lost)
 * @returns Primitive number value
 */
toNumber(): number;

Usage Examples:

const x = new BigNumber(456.789);
x.toNumber();                   // 456.789
+x;                             // 456.789 (unary plus operator)

// Precision loss with large numbers
const big = new BigNumber('45987349857634085409857349856430985');
big.toNumber();                 // 4.598734985763409e+34

// Special values
new BigNumber('Infinity').toNumber();  // Infinity
new BigNumber('NaN').toNumber();       // NaN

// Negative zero preservation
const negZero = new BigNumber(-0);
1 / negZero.toNumber();         // -Infinity
1 / +negZero;                   // -Infinity

JSON Serialization

Provides JSON serialization support (same as valueOf).

/**
 * Returns string representation for JSON serialization
 * @returns String value (same as valueOf)
 */
toJSON(): string;

Usage Examples:

const x = new BigNumber('123.456');
JSON.stringify(x);              // "123.456"
x.toJSON();                     // "123.456"

// Array of BigNumbers
const arr = [new BigNumber('1.1'), new BigNumber('2.2')];
JSON.stringify(arr);            // ["1.1", "2.2"]

Value Of

Returns string representation including negative zero sign.

/**
 * Returns string representation (includes negative zero sign)
 * @returns String value with sign preservation
 */
valueOf(): string;

Usage Examples:

const x = new BigNumber('-0');
x.toString();                   // "0"
x.valueOf();                    // "-0" (preserves negative zero)

const y = new BigNumber('1.777e+457');
y.valueOf();                    // "1.777e+457"
y.toString();                   // "1.777e+457" (same in this case)

Types

interface BigNumber.Format {
  prefix?: string;                    // String to prepend
  suffix?: string;                    // String to append
  decimalSeparator?: string;          // Decimal point character
  groupSeparator?: string;            // Thousands separator for integer part
  groupSize?: number;                 // Primary grouping size for integer part
  secondaryGroupSize?: number;        // Secondary grouping size for integer part
  fractionGroupSeparator?: string;    // Thousands separator for fraction part
  fractionGroupSize?: number;         // Grouping size for fraction part
}

type BigNumber.RoundingMode = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8;

docs

arithmetic.md

comparison.md

configuration.md

constructor.md

conversion.md

index.md

precision.md

tile.json