or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

arithmetic.mdcomparison.mdconfiguration.mdconstructor.mdindex.mdprecision.mdstring-conversion.mdtype-checking.md
tile.json

precision.mddocs/

Precision and Rounding

Control decimal places, significant digits, and rounding behavior for calculations and output formatting.

Capabilities

Decimal Places Control

Get or set the number of decimal places for a BigNumber.

/**
 * Returns the number of decimal places
 * @returns {number} Number of decimal places, or null for ±Infinity/NaN
 */
decimalPlaces(): number

/**
 * Returns BigNumber rounded to specified decimal places
 * @param {number} decimalPlaces - Number of decimal places (0 to 1e9)
 * @param {number} [roundingMode] - Rounding mode (0-8), defaults to ROUNDING_MODE
 * @returns {BigNumber} Rounded BigNumber
 */
decimalPlaces(decimalPlaces, roundingMode): BigNumber

/**
 * Alias for decimalPlaces
 */
dp(): number
dp(decimalPlaces, roundingMode): BigNumber

Usage Examples:

const x = new BigNumber(1234.56);

// Get decimal places
x.decimalPlaces();  // 2
x.dp();            // 2

// Set decimal places with rounding
x.decimalPlaces(1);                     // '1234.6'
x.decimalPlaces(0, BigNumber.ROUND_UP); // '1235'
x.dp(1, BigNumber.ROUND_DOWN);          // '1234.5'

// Very small numbers
const y = new BigNumber('9.9e-101');
y.decimalPlaces();  // 102

// Special values
new BigNumber(Infinity).dp();  // null
new BigNumber(NaN).dp();       // null

Precision Control

Get or set the number of significant digits.

/**
 * Returns the number of significant digits
 * @param {boolean} [includeZeros] - Whether to count trailing zeros in integers
 * @returns {number} Number of significant digits, or null for ±Infinity/NaN
 */
precision(includeZeros): number

/**
 * Returns BigNumber rounded to specified significant digits
 * @param {number} significantDigits - Number of significant digits (1 to 1e9)
 * @param {number} [roundingMode] - Rounding mode (0-8), defaults to ROUNDING_MODE
 * @returns {BigNumber} Rounded BigNumber
 */
precision(significantDigits, roundingMode): BigNumber

/**
 * Alias for precision
 */
sd(includeZeros): number
sd(significantDigits, roundingMode): BigNumber

Usage Examples:

const x = new BigNumber(9876.54321);

// Get precision
x.precision();        // 9 (all digits are significant)
x.sd();              // 9

// Trailing zeros in integers
const y = new BigNumber(987000);
y.precision(false);   // 3 (trailing zeros not counted)
y.precision(true);    // 6 (trailing zeros counted)

// Set precision with rounding
x.precision(6);                        // '9876.54'
x.precision(6, BigNumber.ROUND_UP);    // '9876.55'
x.sd(2);                              // '9900'
x.sd(2, BigNumber.ROUND_DOWN);        // '9800'

Integer Value Extraction

Extract the integer part with specified rounding.

/**
 * Returns BigNumber rounded to integer
 * @param {number} [roundingMode] - Rounding mode (0-8), defaults to ROUNDING_MODE
 * @returns {BigNumber} Integer value
 */
integerValue(roundingMode): BigNumber

Usage Examples:

const x = new BigNumber(123.456);
const y = new BigNumber(-12.7);

// Default rounding (ROUND_HALF_UP)
x.integerValue();                        // '123'
y.integerValue();                        // '-13'

// Specific rounding modes
x.integerValue(BigNumber.ROUND_CEIL);    // '124'
x.integerValue(BigNumber.ROUND_FLOOR);   // '123'
x.integerValue(BigNumber.ROUND_DOWN);    // '123'

y.integerValue(BigNumber.ROUND_CEIL);    // '-12'
y.integerValue(BigNumber.ROUND_FLOOR);   // '-13'
y.integerValue(BigNumber.ROUND_DOWN);    // '-12'

// Already integer
new BigNumber(42).integerValue();        // '42'

Decimal Point Shifting

Shift the decimal point by specified positions.

/**
 * Returns BigNumber with decimal point shifted
 * @param {number} n - Number of positions to shift (-9007199254740991 to 9007199254740991)
 * @returns {BigNumber} Shifted BigNumber
 */
shiftedBy(n): BigNumber

Usage Examples:

const x = new BigNumber(1.23);

// Shift right (multiply by power of 10)
x.shiftedBy(3);   // '1230'
x.shiftedBy(1);   // '12.3'

// Shift left (divide by power of 10)
x.shiftedBy(-3);  // '0.00123'
x.shiftedBy(-1);  // '0.123'

// Large shifts
const big = new BigNumber('1.23e100');
big.shiftedBy(50);   // '1.23e150'
big.shiftedBy(-50);  // '1.23e50'

// No change
x.shiftedBy(0);   // '1.23'

Rounding Modes

BigNumber supports 9 different rounding modes:

// Rounding mode constants
BigNumber.ROUND_UP = 0;         // Away from zero
BigNumber.ROUND_DOWN = 1;       // Towards zero
BigNumber.ROUND_CEIL = 2;       // Towards +Infinity
BigNumber.ROUND_FLOOR = 3;      // Towards -Infinity
BigNumber.ROUND_HALF_UP = 4;    // To nearest, away from zero if tied
BigNumber.ROUND_HALF_DOWN = 5;  // To nearest, towards zero if tied
BigNumber.ROUND_HALF_EVEN = 6;  // To nearest, to even if tied (banker's rounding)
BigNumber.ROUND_HALF_CEIL = 7;  // To nearest, towards +Infinity if tied
BigNumber.ROUND_HALF_FLOOR = 8; // To nearest, towards -Infinity if tied

Rounding Examples:

const x = new BigNumber(2.5);
const y = new BigNumber(-2.5);

// Different rounding modes
x.integerValue(BigNumber.ROUND_UP);        // '3'
x.integerValue(BigNumber.ROUND_DOWN);      // '2'
x.integerValue(BigNumber.ROUND_CEIL);      // '3'
x.integerValue(BigNumber.ROUND_FLOOR);     // '2'
x.integerValue(BigNumber.ROUND_HALF_UP);   // '3'
x.integerValue(BigNumber.ROUND_HALF_DOWN); // '2'
x.integerValue(BigNumber.ROUND_HALF_EVEN); // '2' (even)

y.integerValue(BigNumber.ROUND_UP);        // '-3'
y.integerValue(BigNumber.ROUND_DOWN);      // '-2'
y.integerValue(BigNumber.ROUND_CEIL);      // '-2'
y.integerValue(BigNumber.ROUND_FLOOR);     // '-3'

Global Configuration

Precision and rounding behavior can be configured globally:

// Set global defaults
BigNumber.config({
  DECIMAL_PLACES: 20,           // Max decimal places for division
  ROUNDING_MODE: BigNumber.ROUND_HALF_UP,  // Default rounding mode
  POW_PRECISION: 0              // Max precision for power operations
});

// Check current configuration
const config = BigNumber.config();
console.log(config.DECIMAL_PLACES);  // 20
console.log(config.ROUNDING_MODE);   // 4

Advanced Precision Scenarios

Working with Very Small Numbers

const tiny = new BigNumber('1e-1000');

tiny.precision();           // 1
tiny.decimalPlaces();       // 1000
tiny.shiftedBy(500);        // '1e-500'

// Precision is maintained
const result = tiny.multipliedBy('1e500');  // '1e-500'
result.precision();         // 1

High-Precision Calculations

// Configure for high precision
BigNumber.config({ DECIMAL_PLACES: 50 });

const pi = new BigNumber('3.1415926535897932384626433832795028841971693993751');
const radius = new BigNumber('10');

const circumference = pi.multipliedBy(2).multipliedBy(radius);
circumference.precision();    // Full precision maintained
circumference.decimalPlaces(); // Up to 50 decimal places

Precision Loss Detection

function detectPrecisionLoss(operation) {
  const before = operation.input.precision();
  const after = operation.result.precision();
  
  if (after < before) {
    console.log(`Precision reduced from ${before} to ${after} digits`);
  }
  
  return operation.result;
}

// Usage
const x = new BigNumber('1.23456789012345678901234567890');
BigNumber.config({ DECIMAL_PLACES: 10 });
const result = x.dividedBy(3);  // May reduce precision due to division limit

Performance Considerations

  • Precision operations are optimized for common cases
  • Very high precision settings may impact performance
  • Rounding is performed only when necessary
  • Decimal place calculations are cached when possible