Options
All
  • Public
  • Public/Protected
  • All
Menu

Class MathContext

Immutable objects which encapsulate the context settings which describe certain rules for numerical operators, such as those implemented by the BigDecimal class.

The base-independent settings are:

  • precision: the number of digits to be used for an operation; results are rounded to this precision
  • roundingMode: a RoundingMode object which specifies the algorithm to be used for rounding.

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.
}

Hierarchy

  • MathContext

Index

Constructors

constructor

Properties

Readonly precision

precision: number

The number of digits to be used for an operation. A value of 0 indicates that unlimited precision (as many digits as are required) will be used. Note that leading zeros (in the coefficient of a number) are never significant.

precision will always be non-negative.

Readonly roundingMode

roundingMode: RoundingMode

The rounding algorithm to be used for an operation. By default it is HALF_UP.

Static DECIMAL128

DECIMAL128: MathContext = ...

A MathContext object with a precision setting matching the precision of the IEEE 754-2019 decimal128 format, 34 digits, and a rounding mode of HALF_EVEN. Note the exponent range of decimal64 is not used for rounding.

Static DECIMAL32

DECIMAL32: MathContext = ...

A MathContext object with a precision setting matching the precision of the IEEE 754-2019 decimal32 format, 7 digits, and a rounding mode of HALF_EVEN. Note the exponent range of decimal32 is not used for rounding.

Static DECIMAL64

DECIMAL64: MathContext = ...

A MathContext object with a precision setting matching the precision of the IEEE 754-2019 decimal64 format, 16 digits, and a rounding mode of HALF_EVEN. Note the exponent range of decimal64 is not used for rounding.

Static UNLIMITED

UNLIMITED: MathContext = ...

A MathContext object whose settings have the values required for unlimited precision arithmetic. The values of the settings are: precision=0 roundingMode=HALF_UP

Generated using TypeDoc