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.

    The values passed must match one of Java BigDecimal's constructors, so the valid usages of this function is listed below:

    Big(123n); // bigint, 123
    Big(123n, 3); // bigint and scale, 0.123
    Big(123n, 3, MC(2, RoundingMode.HALF_UP)); // bigint, scale and mc, 0.12
    Big(123n, undefined, MC(2, RoundingMode.HALF_UP)); // bigint and mc, 1.2E+2
    Big('1.13e12'); // string, 1.13E+12
    Big('1.11e11', undefined, MC(2, RoundingMode.HALF_UP)); // string and mc, 1.1E+11
    Big(10000); // number, 10000
    Big(123, 5); // integer and scale, 0.00123
    Big(1.1233, undefined, MC(2, RoundingMode.HALF_UP)); // number and scale, 1.1

    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.MAX_VALUE, Number.MAX_VALUE]
      • Both a scale and a math context is provided. You can only give one of scale and math context. Passing undefined is same as omitting an argument.
      • If value is a double and scale is given.
    • 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.
    • If value is not a BigInt or number, and scale is given.

    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

    • Optional roundingMode: RoundingMode

      Optional rounding Mode. By default RoundingMode.HALF_UP.

    Returns MathContext

Generated using TypeDoc