core-arbitrary-precision

3.1.1 • Public • Published

core-arbitrary-precision

Build Status Coverage Status Code Climate

Abstraction for core arbitrary precision functionality in big.js, bignumber.js, decimal.js and others via adapters.

Install

npm i core-arbitrary-precision

Usage

Pending CodePen example.

Adapters

See up to date list.

Factory and configuration

var decimalFactory = require('core-arbitrary-precision');
var adapter = require('bigjs-adapter'); // See adapters section for full list
 
var Decimal = decimalFactory(adapter);
 
Decimal.getPrecision(); // => 20
Decimal.setPrecision(5);
Decimal.getPrecision(); // => 5

Operations

This core package has no operations, but they can be added:

new Decimal('2').pow(new Decimal('3')).valueOf(); // => Error!
 
var Decimal = require('pow-arbitrary-precision')(Decimal);
new Decimal('2').pow(new Decimal('3')).valueOf(); // => '8'

toString, valueOf and toJSON

var decimalOne = new Decimal('1');
 
// with bigjs-adapter (other adapters might have differing implementations)
decimalOne.toString() === decimalOne.valueOf() === decimalOne.toJSON(); // => true
 
Number(decimalThird); // => 1

JSON.stringify and JSON.parse with reviver

var decimalOne = new Decimal('1');
 
var stringified = JSON.stringify(decimalOne); // => '"1"'
 
JSON.parse(stringified, decimalOne.reviver); // => new decimalOne('1')

See spec.

Related projects

Package Sidebar

Install

npm i core-arbitrary-precision

Weekly Downloads

34

Version

3.1.1

License

MIT

Last publish

Collaborators

  • javiercejudo