Arbitrary-length decimal implementation using JavaScript's native BigInt with no dependencies.
- Supported on Node >= 10.4 and Deno.
- Check caniuse.com for browser support.
-
Deno module first. Soon to be available as ES Module (ESM) and CommonJS (Node) module.
-
Compute methods are largely implemented through native BigInt, without much string manipulation required.
-
Standalone & lightweight. Zero dependencies.
-
Intuitive data structure – base amount and decimal places, similar to that of cryptocurrency esp. Bitcoin.
-
API is similar with the popular BigNumber libraries such as bignumber.js, big.js, decimal.js. Not all methods are supported, yet.
import { BigDenary } from "https://deno.land/x/bigdenary/mod.ts";
const bd = new BigDenary("123.4512");
const sum = bd.add(56.1e2);
console.log(sum.toString()); // 5733.4512
console.log(sum); // BigDenary { base: 57334512n, _decimals: 4 }
API is largely inspired by and attempts to be compatible with decimal.js-light
.
constructor()
: supportstype BDNumberInput = number | string | bigint | BigDenary | BigDenaryRaw
.toString()
: Returnsstring
representationvalueOf()
: Returnsnumber
approximationtoFixed(digits?)
: Returnsstring
representation to the number ofdigits
to appear decimal point.
plus()
oradd()
minus()
orsub()
multipliedBy()
ormul()
dividedBy()
ordiv()
negated()
orneg()
absoluteValue()
orabs()
comparedTo()
orcmp()
equals()
oreq()
greaterThan()
orgt()
greaterThanOrEqualTo()
orgte()
lessThan()
orlt()
lessThanOrEqualTo()
orlte()
-
Install Deno
-
Run unit tests
deno test
JavaScript native decimal support is currently being proposed (Stage 1) to ECMA.
MIT · U-Zyn Chua
Contributions are welcomed.