Multiple output formats including fixed-point, exponential, precision notation, and custom formatting for BigNumber instances.
Converts BigNumber to string representation with optional base conversion.
/**
* Returns string representation of this BigNumber
* @param {number} [base] - Base for conversion (2-36 or alphabet length)
* @returns {string} String representation
*/
toString(base): stringUsage Examples:
const x = new BigNumber(750000);
// Default base 10
x.toString(); // '750000'
// Different bases
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'
// Large numbers with exponential notation
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, rounding applied)Returns string in normal (fixed-point) notation with specified decimal places.
/**
* Returns fixed-point notation string
* @param {number} [decimalPlaces] - Number of decimal places (0 to 1e9)
* @param {number} [roundingMode] - Rounding mode (0-8)
* @returns {string} Fixed-point string
*/
toFixed(decimalPlaces, roundingMode): stringUsage Examples:
const x = new BigNumber(3.456);
// Default behavior (no decimal places specified)
x.toFixed(); // '3.456'
// Specific decimal places
x.toFixed(0); // '3'
x.toFixed(2); // '3.46'
x.toFixed(5); // '3.45600'
// With rounding mode
x.toFixed(2, BigNumber.ROUND_DOWN); // '3.45'
x.toFixed(2, BigNumber.ROUND_UP); // '3.46'
// Large numbers (always normal notation)
const large = new BigNumber('1.23e+21');
large.toFixed(); // '1230000000000000000000'
// JavaScript Number.toFixed() vs BigNumber.toFixed()
const jsNum = 3.456;
jsNum.toFixed(); // '3' (JavaScript rounds to 0 decimal places)
new BigNumber(3.456).toFixed(); // '3.456' (BigNumber preserves precision)Returns string in exponential notation with specified decimal places.
/**
* Returns exponential notation string
* @param {number} [decimalPlaces] - Decimal places after first digit (0 to 1e9)
* @param {number} [roundingMode] - Rounding mode (0-8)
* @returns {string} Exponential notation string
*/
toExponential(decimalPlaces, roundingMode): stringUsage Examples:
const x = new BigNumber(45.6);
// Default (minimum digits needed)
x.toExponential(); // '4.56e+1'
// Specific decimal places
x.toExponential(0); // '5e+1'
x.toExponential(1); // '4.6e+1'
x.toExponential(3); // '4.560e+1'
// With rounding
x.toExponential(1, BigNumber.ROUND_DOWN); // '4.5e+1'
x.toExponential(1, BigNumber.ROUND_UP); // '4.6e+1'
// Very large/small numbers
const big = new BigNumber('1.23e+100');
big.toExponential(2); // '1.23e+100'
const small = new BigNumber('1.23e-100');
small.toExponential(2); // '1.23e-100'Returns string rounded to specified significant digits.
/**
* Returns precision notation string
* @param {number} [significantDigits] - Significant digits (1 to 1e9)
* @param {number} [roundingMode] - Rounding mode (0-8)
* @returns {string} Precision notation string
*/
toPrecision(significantDigits, roundingMode): stringUsage Examples:
const x = new BigNumber(45.6);
// Default (same as toString)
x.toPrecision(); // '45.6'
// Specific significant digits
x.toPrecision(1); // '5e+1' (exponential when needed)
x.toPrecision(2); // '46'
x.toPrecision(5); // '45.600'
// With rounding
const y = new BigNumber(45.6);
y.toPrecision(2, BigNumber.ROUND_UP); // '46'
y.toPrecision(2, BigNumber.ROUND_DOWN); // '45'
// Large numbers
const large = new BigNumber(1234567);
large.toPrecision(3); // '1.23e+6'
large.toPrecision(10); // '1234567.000'Returns formatted string with custom separators and grouping.
/**
* Returns formatted string with custom separators
* @param {number} [decimalPlaces] - Decimal places (0 to 1e9)
* @param {number} [roundingMode] - Rounding mode (0-8)
* @param {object} [format] - Formatting options
* @returns {string} Formatted string
*/
toFormat(decimalPlaces, roundingMode, format): string
toFormat(decimalPlaces, format): string
toFormat(format): stringFormat Object Properties:
interface Format {
prefix?: string; // String to prepend
decimalSeparator?: string; // Decimal separator
groupSeparator?: string; // Integer grouping separator
groupSize?: number; // Primary grouping size
secondaryGroupSize?: number; // Secondary grouping size
fractionGroupSeparator?: string; // Fraction grouping separator
fractionGroupSize?: number; // Fraction grouping size
suffix?: string; // String to append
}Usage Examples:
const x = new BigNumber('123456789.123456789');
// Default formatting
x.toFormat(); // '123,456,789.123456789'
// Custom format object
const fmt = {
decimalSeparator: ',',
groupSeparator: '.',
groupSize: 3,
secondaryGroupSize: 2
};
x.toFormat(fmt); // '12.34.56.789,123456789'
x.toFormat(2, fmt); // '12.34.56.789,12'
x.toFormat(3, BigNumber.ROUND_UP, fmt); // '12.34.56.789,124'
// Global format configuration
BigNumber.config({
FORMAT: {
groupSeparator: ' ',
decimalSeparator: ',',
groupSize: 3,
fractionGroupSize: 5,
fractionGroupSeparator: ' '
}
});
x.toFormat(); // '123 456 789,12345 6789'
// Currency formatting
const currency = {
prefix: '$',
groupSeparator: ',',
groupSize: 3,
suffix: ' USD'
};
new BigNumber(1234.56).toFormat(2, currency); // '$1,234.56 USD'Converts BigNumber to fraction representation.
/**
* Returns fraction as array of [numerator, denominator]
* @param {string|number|BigNumber} [maxDenominator] - Maximum denominator value
* @returns {[BigNumber, BigNumber]} Array of [numerator, denominator]
*/
toFraction(maxDenominator): [BigNumber, BigNumber]Usage Examples:
const x = new BigNumber(1.75);
// Exact fraction
x.toFraction(); // [new BigNumber('7'), new BigNumber('4')]
// With maximum denominator
const pi = new BigNumber('3.14159265358');
pi.toFraction(); // [new BigNumber('157079632679'), new BigNumber('50000000000')]
pi.toFraction(100000); // [new BigNumber('312689'), new BigNumber('99532')]
pi.toFraction(10000); // [new BigNumber('355'), new BigNumber('113')]
pi.toFraction(100); // [new BigNumber('311'), new BigNumber('99')]
pi.toFraction(10); // [new BigNumber('22'), new BigNumber('7')]
pi.toFraction(1); // [new BigNumber('3'), new BigNumber('1')]
// Working with results
const [num, den] = pi.toFraction(113);
const decimal = num.dividedBy(den); // Approximate original valueConverts BigNumber to JavaScript primitive types.
/**
* Converts to JavaScript number (may lose precision)
* @returns {number} JavaScript number
*/
toNumber(): number
/**
* Returns string representation (includes minus sign for negative zero)
* @returns {string} String value
*/
valueOf(): string
/**
* JSON serialization (same as valueOf)
* @returns {string} JSON string
*/
toJSON(): stringUsage Examples:
const x = new BigNumber(456.789);
const large = new BigNumber('45987349857634085409857349856430985');
const negZero = new BigNumber('-0');
// Number conversion
x.toNumber(); // 456.789
+x; // 456.789 (unary plus operator)
large.toNumber(); // 4.598734985763409e+34 (precision loss)
// String conversion with valueOf
x.valueOf(); // '456.789'
negZero.toString(); // '0'
negZero.valueOf(); // '-0' (preserves negative zero)
// JSON serialization
JSON.stringify(x); // '"456.789"'
x.toJSON(); // '456.789'
// Type coercion
x == '456.789'; // true (string comparison)
x === 456.789; // false (type mismatch)const nan = new BigNumber(NaN);
const inf = new BigNumber(Infinity);
const negInf = new BigNumber(-Infinity);
nan.toString(); // 'NaN'
inf.toString(); // 'Infinity'
negInf.toString(); // '-Infinity'
// All formatting methods handle special values
nan.toFixed(2); // 'NaN'
inf.toExponential(2); // 'Infinity'
negInf.toPrecision(5); // '-Infinity'const veryLarge = new BigNumber('1e+1000');
const verySmall = new BigNumber('1e-1000');
veryLarge.toString(); // '1e+1000'
verySmall.toString(); // '1e-1000'
// Fixed notation for extreme values
veryLarge.toFixed(); // '1000...000' (1000 zeros)
verySmall.toFixed(); // '0.000...001' (999 zeros before 1)String conversion behavior is affected by global settings:
BigNumber.config({
EXPONENTIAL_AT: [-7, 20], // Controls when exponential notation is used
DECIMAL_PLACES: 20, // Affects base conversion rounding
FORMAT: { /* formatting options */ } // Default format for toFormat()
});toString() is optimized for common bases (2, 8, 10, 16)