Options
All
  • Public
  • Public/Protected
  • All
Menu

bigdecimal.js

Index

Enumerations

Classes

Functions

Functions

Const Big

  • Constructor function for BigDecimal. Can be invoked with new or without new.

    Sample Usage:

    const { Big } = require('bigdecimal.js');
    
    // Constructor accepts any value such as string and BigDecimal itself:
    
    const x = Big('1.1111111111111111111111');
    const y = Big(x);
    
    const z = x.add(y);
    console.log(z.toString()); // 2.2222222222222222222222
    
    
    const u = Big(1.1);
    const v = Big(2n);
    
    // You can also construct a BigDecimal from a number or a BigInt:
    
    console.log(u.toString()); // 1.1
    console.log(v.toString()); // 2
    
    throws

    RangeError on following situations:

    • If value is a number:
      • Value is not in the range [Number.MIN_VALUE, Number.MAX_VALUE]
      • Both scale and precision is provided. You can only give one of scale and mc. Passing undefined is same as omitting.
    • If value is not a number, a BigInt or a BigDecimal, it will be converted to string. An error will be thrown if the string format is invalid.

    Parameters

    • n: any

      Any value to build a BigDecimal from. Types other than Number, BigInt and BigDecimal will be internally converted to string and parsed.

    • Optional scale: number

      Scale to use, by default 0.

    • Optional mc: MathContext

      MathContext object which allows you to set precision and rounding mode.

    Returns BigDecimal

Const MC

  • Constructor function for MathContext. Can be invoked with new or without new.

    Sample Usage:

    const { Big, MathContext, RoundingMode } = require('bigdecimal.js');
    
    const x = Big('1');
    const y = Big('3');
    
    const res1 = x.divide(y, new MathContext(3));
    console.log(res1.toString()); // 0.333
    
    const res2 = x.divide(y, MathContext(3, RoundingMode.UP)); // You can also use without `new` operator
    console.log(res2.toString()); // 0.334
    
    try {
        x.divide(y);
        // throws since full precision is requested but it is not possible
    } catch (e) {
        console.log(e); // RangeError: Non-terminating decimal expansion; no exact representable decimal result.
    }
    

    Parameters

    • precision: number

      Precision value

    • roundingMode: RoundingMode

      Rounding Mode

    Returns MathContext

Generated using TypeDoc