BigDenary
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.
Features
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.
Usage
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
.
Available API
Core
constructor()
: supportstype BDNumberInput = number | string | bigint | BigDenary | BigDenaryRaw
.toString()
: Returnsstring
representationvalueOf()
: Returnsnumber
approximationtoFixed(digits?)
: Returnsstring
representation to the number ofdigits
to appear decimal point.
Operations
plus()
oradd()
minus()
orsub()
multipliedBy()
ormul()
dividedBy()
ordiv()
negated()
orneg()
absoluteValue()
orabs()
Comparisons
comparedTo()
orcmp()
equals()
oreq()
greaterThan()
orgt()
greaterThanOrEqualTo()
orgte()
lessThan()
orlt()
lessThanOrEqualTo()
orlte()
Develop and running of tests
Install Deno
Run unit tests
deno test
Notes
JavaScript native decimal support is currently being proposed (Stage 1) to ECMA.
License
MIT · U-Zyn Chua
Contributions are welcomed.