vdt / node-decimal

decimal library for node.js supporting large numbers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

This is a decimal library for node.js which supports arbitrarily large numbers built on top of https://github.com/substack/node-bigint

install

You will need libgmp3 development package installed to install node-decimal. On debian systems this is the libgmp3-dev package.

npm install git://github.com/bitfloor/node-decimal.git

basic usage

var decimal = require('decimal');

// the 'deciaml' module exposes methods like 'add', 'sub', 'mul'
// each of these methods returns a Decimal object and you can chain the methods
var num = decimal.add(1.2, 2.3).add(0.2).toNumber();

// The toNumber() and toString() methods will return the respective native types
// Note: If the number outside the precision of the native Number type
// you should be using toString to store it and preserve the precision
var result = decimal.add(1.2, 2.3).toString(); // yeilds '2.5'

// All of the operator methods also support string input
// this is how you can preserve precision if exceeding the capabilities of the native types
var result = decimal.add('1.2', '2.3').toString(); // yeilds '2.5'

About

decimal library for node.js supporting large numbers

License:MIT License