Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

js-big-decimal

Package Overview
Dependencies
Maintainers
1
Versions
39
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

js-big-decimal

Work with large numbers on the client side. Round them off to any required precission.

  • 1.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
48K
increased by4.3%
Maintainers
1
Weekly downloads
 
Created
Source

JS Big Decimal

Travis license npm

Work with large numbers on the client side with high precision.

Installation

npm install --save js-big-decimal

Usage

Require in javascript as

var bigDecimal = require('js-big-decimal').bigDecimal;

For typescript, use

import { bigDecimal } from 'js-big-decimal';

Operations

bigDecimal(number)

Create a new objet of type BigDecimal. Supports parameters of type number and string. If string passed cannot be parsed as a number error is thrown. It is recommended to use string as it circumvents the issue of precision with JS native float implementation and max limit for integer.

It supports exponentiation, but only with integral exponent.

var n1 = new bigDecimal(12.6789);
var n2 = new bigDecimal("12345.6789");
var n3 = new bigDecimal('12.456e3'); // 12456

getValue()

Returns the string value of the decimal.

console.log(n2.getValue()); // "12345.6789"

getPrettyValue(number, digits, separator)

By default this returns the number in standard number format, comma after every three digts. Both arguments, digits - the number of digits (of the integral part) to group by, and separator - the character to mark the separation. Example of this can be to format a 16 digit number as credit card.

var value = bigDecimal.getPrettyValue("12345.6789"); // value = "12,345.6789"

Alternately, use the instance property. It returns the result as string.

var n3 = n2.getPrettyValue(); // n4 = "12,345.6789"

var num = new bigDecimal(1234567890123456)
var card = num.getPrettyValue(4, '-'); // cardNumber = "1234-5678-9012-3456"

round(number, precision)

Returns the rounded value to the specified precision (number of digits after decimal). The default is set to 0 if no argument is passed.

var value = bigDecimal.round("123.678", 2); // value = "123.68"

Alternately, use the instance property. It returns the result as bigDecimal.

var n3 = n1.round(2); // n3 = new bigDecimal("12.68")
var n4 = n2.round(); // n4 = new bigDecimal("12346")

compareTo(number1, number2)

Compare two numbers. Returns 1, 0 and -1 if number1 > number2, number1 == number2 and number1 < number2 respectively.

var value = bigDecimal.compareTo("23.678", "67.34"); // value = -1
var value = bigDecimal.compareTo("23.678", "23.6780"); // value = 0
var value = bigDecimal.compareTo("123.678", "67.34"); // value = 1

Alternately, use the instance property. It returns the result as Integer.

var n1 = new bigDecimal('1234');
var n2 = new bigDecimal('8765');
var value = n1.compareTo(n2); // value = -1

negate(number)

Returns negation of a given number.

var value = bigDecimal.negate("123.678"); // value = "-123.678";

Alternately, use the instance property. It returns the result as new bigDecimal.

var n = new bigDecimal('-1234');
var value = n.negate(); // value = new bigDecimal('1234')

add(augend, addend)

Add two numbers. Pass in negative for substraction. Ensure parameters are strings.

var sum = bigDecimal.add("23.678", "67.34"); // sum = "91.018"
var diff = bigDecimal.add("67.34", "-23.678"); // diff = "43.662"

Alternately, use the instance property. It returns the result as new bigDecimal.

var n1 = new bigDecimal('1234');
var n2 = new bigDecimal('8765');
var sum = n1.add(n2); // sum = new bigDecimal('9999')

subtract(minuend, subtrahend)

Subtract one number from another

var diff = bigDecimal.subtract("67.34", "23.678"); // diff = "43.662"

Alternately, use the instance property. It returns the result as new bigDecimal.

var n1 = new bigDecimal('12.67');
var n2 = new bigDecimal('130.7');
var diff = n1.subtract(n2); // diff = new bigDecimal('-118.03')

multiply(multiplicand, multiplier)

Multiply two numbers. Ensure parameters are strings.

var product = bigDecimal.multiply("-0.13", "0.00130"); // product = "-0.000169"

Alternately, use the instance property. It returns the result as new bigDecimal.

var n1 = new bigDecimal('-0.13');
var n2 = new bigDecimal('0.00130');
var product = n1.multiply(n2); // product = new bigDecimal('-0.000169')

divide(dividend, divisor, precision)

Divide two numbers. Pass arguments as string if calling on bigDecimal or pass an instance of bigDecimal if calling on object. precision is an optional parameter with default value of 8.

var quotient = bigDecimal.divide('45', '4', 2); // quotient = 11.25

Alternately, use the instance property. It returns the result as new bigDecimal.

var n1 = new bigDecimal('45');
var n2 = new bigDecimal('4');
var quotient = n1.divide(n2); // quotient = new bigDecimal('11.25')

Keywords

FAQs

Package last updated on 13 Jul 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc