Any value to build a BigDecimal from. Types other than Number
, BigInt
and BigDecimal
will be internally
converted to string and parsed.
Scale to use, by default 0.
MathContext object which allows you to set precision and rounding mode.
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.
}
Precision value
Optional rounding Mode. By default RoundingMode.HALF_UP.
Generated using TypeDoc
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:
Sample Usage:
RangeError on following situations:
[-Number.MAX_VALUE, Number.MAX_VALUE]
undefined
is same as omitting an argument.number
, aBigInt
or aBigDecimal
, it will be converted to string. An error will be thrown if the string format is invalid.BigInt
ornumber
, and scale is given.