a polyfill for decimal propocal - https://github.com/tc39/proposal-decimal It is implemented on the top of native BigInt. It can be compiled using https://www.npmjs.com/package/babel-plugin-transform-bigint to use JSBI.
npm install @yaffle/bigdecimal
BigDecimal.BigDecimal(string)
BigDecimal.BigDecimal(bigint)
BigDecimal.BigDecimal(number) (only integers)
a.toString()
a.toFixed(fractionDigits)
a.toPrecision(precision)
a.toExponential(fractionDigits)
BigDecimal.toBigInt(a)
(not in the spec)
BigDecimal.toNumber(a)
(not in the spec, only integers)
BigDecimal.unaryMinus(a)
BigDecimal.add(a, b[, rounding])
BigDecimal.subtract(a, b[, rounding])
BigDecimal.multiply(a, b[, rounding])
BigDecimal.divide(a, b, rounding)
BigDecimal.round(a, rounding)
BigDecimal.equal(a, b)
BigDecimal.lessThan(a, b)
BigDecimal.greaterThan(a, b)
BigDecimal.log(a, rounding)
BigDecimal.exp(a, rounding)
BigDecimal.sin(a, rounding)
BigDecimal.cos(a, rounding)
BigDecimal.atan(a, rounding)
The rounding
argument may look like {maximumFractionDigits: 10, roundingMode: "half-even"}
or {maximumSignificantDigits: 10, roundingMode: "half-even"}
, where the roundingMode can be "floor", or "ceil", or "half-even", or "half-up".
Similar to BigDecimal, but uses base 2.
import {BigDecimal} from "./node_modules/@yaffle/bigdecimal/BigDecimal.js";
var pi = BigDecimal.multiply(BigDecimal.BigDecimal(4), BigDecimal.atan(BigDecimal.BigDecimal(1), {maximumSignificantDigits: 1000, roundingMode: "half-even"}));
console.log(pi.toString());
Consider to use only "half-even" rounding mode and rounding to a maximum number of significant digits for floating-point arithmetic,
or only "floor" rounding to a maximum number of fraction digits for fixed-point arithmetic.
BigFloat may have better performance.
Use to round to an integer BigDecimal.round(a, {maximumFractionDigits: 0, roundingMode: "half-even"})
.