JS Big Decimal
Work with large numbers on the client side with high precision.
Contents
- Installation
- Operations
- bigDecimal(number)
- getValue()
- getPrettyValue(number, digits, separator)
- round(number, precision, roundingMode)
- floor(number)
- ceil(number)
- compareTo(number1, number2)
- negate(number)
- add(augend, addend)
- subtract(minuend, subtrahend)
- multiply(multiplicand, multiplier)
- divide(dividend, divisor, precision)
- modulus(dividend, divisor)
Installation
npm install --save js-big-decimal
Usage
:heavy_exclamation_mark: Note: Usage has changed since version 1.1.4
Require in javascript
as
var bigDecimal = require('js-big-decimal');
For typescript
, use
import bigDecimal require('js-big-decimal');
For web
, when used with script tag, a variable on Window
object is created.
<script src="node_modules/js-big-decimal/dist/web/js-big-decimal.min.js"></script>
console.log(bigDecimal.add('12', '45'));
Operations
bigDecimal(number)
Create a new object 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');
getValue()
Returns the string value of the decimal.
console.log(n2.getValue());
getPrettyValue(number, digits, separator)
By default this returns the number in standard number format, comma after every three digits. 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");
Alternately, use the instance property. It returns the result as string
.
var n3 = n2.getPrettyValue();
var num = new bigDecimal(1234567890123456)
var card = num.getPrettyValue(4, '-');
round(number, precision, roundingMode)
Returns the rounded value to the specified precision (number of digits after decimal). The default precision is set to 0 and rounding mode set to HALF_EVEN
if no argument is passed.
var value = bigDecimal.round("123.678", 2);
Alternately, use the instance property. It returns the result as bigDecimal
.
var n3 = n1.round(2);
var n4 = n2.round();
Passing in a negative argument for digits to round off to returns the nearest multiple of power of 10. If the magnitude of the argument is larger than or equal to the number of digits in the integral part of the number to round, zero
is returned.
var val1 = bigDecimal.round("123.78", -2);
var val2 = bigDecimal.round("587", -1);
var val3 = bigDecimal.round("123.78", -4);
Round also supports the following rounding modes (These are same as that of Java 8):
CEILING
- Rounding mode to round towards positive infinity.DOWN
- Rounding mode to round towards zero.FLOOR
- Rounding mode to round towards negative infinity.HALF_DOWN
- Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round down.HALF_EVEN
- Rounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor.HALF_UP
- Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up.UNNECESSARY
(!Not Implemented!)- Rounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.UP
- Rounding mode to round away from zero.
Extensive description of the modes can be found at Rounding Modes
var num = new bigDecimal("123.657");
var numRound1 = num.round(1, bigDecimal.RoundingModes.DOWN);
var numRound2 = num.round(2, bigDecimal.RoundingModes.CEILING);
floor(number)
Returns the whole number nearest but not greater than the input number.
var n1 = bigDecimal.floor(12.8)
var n2 = bigDecimal.floor(-12.3)
The instance function returns the result as a new bigDecimal
var n1 = new bigDecimal(12.8).floor()
var n2 = bigDecimal(-12.3).floor()
ceil(number)
Returns the whole number nearest but not lesser than the input number.
var n1 = bigDecimal.ceil(12.8)
var n2 = bigDecimal.ceil(-12.3)
The instance function returns the result as a new bigDecimal
var n1 = new bigDecimal(12.8).ceil()
var n2 = bigDecimal(-12.3).ceil()
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");
var value = bigDecimal.compareTo("23.678", "23.6780");
var value = bigDecimal.compareTo("123.678", "67.34");
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);
negate(number)
Returns negation of a given number.
var value = bigDecimal.negate("123.678");
Alternately, use the instance property. It returns the result as new bigDecimal
.
var n = new bigDecimal('-1234');
var value = n.negate();
add(augend, addend)
Add two numbers. Pass in negative for subtraction. Ensure parameters are string
s.
var sum = bigDecimal.add("23.678", "67.34");
var diff = bigDecimal.add("67.34", "-23.678");
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);
subtract(minuend, subtrahend)
Subtract one number from another
var diff = bigDecimal.subtract("67.34", "23.678");
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);
multiply(multiplicand, multiplier)
Multiply two numbers. Ensure parameters are string
s.
var product = bigDecimal.multiply("-0.13", "0.00130");
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);
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);
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);
modulus(dividend, divisor)
Get the modulus of two numbers, i.e., remainder when the dividend is divided by the divisor. Note that both divisor and dividend need to be integers.
var remainder = bigDecimal.modulus('45', '4');
Alternately, use the instance property. It returns the result as new bigDecimal
.
var n1 = new bigDecimal('45');
var n2 = new bigDecimal('4');
var remainder = n1.modulus(n2);
Further, the result takes the sign of the dividend and the sign of the divisor is ignored. Note that this behaviour is the same as in Java and JavaScript.