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(): supports type BDNumberInput = number | string | bigint | BigDenary | BigDenaryRaw.
  • toString(): Returns string representation
  • valueOf(): Returns number approximation
  • toFixed(digits?): Returns string representation to the number of digits to appear decimal point.

Operations

  • plus() or add()
  • minus() or sub()
  • multipliedBy() or mul()
  • dividedBy() or div()
  • negated() or neg()
  • absoluteValue() or abs()

Comparisons

  • comparedTo() or cmp()
  • equals() or eq()
  • greaterThan() or gt()
  • greaterThanOrEqualTo() or gte()
  • lessThan() or lt()
  • lessThanOrEqualTo() or lte()

Develop and running of tests

  1. Install Deno

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