Installations
npm install js-big-decimal
Developer Guide
Typescript
Yes
Module System
CommonJS, ESM
Node Version
16.20.2
NPM Version
8.19.4
Releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (96.08%)
JavaScript (3.92%)
Developer
Download Statistics
Total Downloads
5,755,652
Last Day
9,094
Last Week
41,955
Last Month
187,713
Last Year
2,168,133
GitHub Statistics
157 Stars
332 Commits
28 Forks
4 Watching
8 Branches
12 Contributors
Bundle Size
9.30 kB
Minified
3.08 kB
Minified + Gzipped
Package Meta Information
Latest Version
2.2.0
Package Id
js-big-decimal@2.2.0
Unpacked Size
198.40 kB
Size
34.64 kB
File Count
31
NPM Version
8.19.4
Node Version
16.20.2
Publised On
29 Dec 2024
Total Downloads
Cumulative downloads
Total Downloads
5,755,652
Last day
-12.4%
9,094
Compared to previous day
Last week
-15.4%
41,955
Compared to previous week
Last month
5.5%
187,713
Compared to previous month
Last year
3.6%
2,168,133
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
JS Big Decimal
Work with large numbers on the client side with high precision
Welcome to a saner world where 0.1 + 0.2 = 0.3
and not 0.30000000000000004
Contents
- Contributors Welcome!
- Installation
- Operations
- bigDecimal(number)
- getValue()
- setValue()
- getPrettyValue(number, digits, separator)
- round(number, precision, roundingMode)
- stripTrailingZero(number)
- abs(number)
- 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)
- Support the developers :heart: :star: :money_with_wings:
Contributors Welcome!
Hi, this is a relatively simple library that solves a very common and frustrating JS issue. With my current workload, it is becoming increasingly difficult to maintain this alone. If you have some basic JS/TS working knowledge, please go thorugh the open bugs/enhancements and help clear the backlog. Thanks in advance! :relieved:
Installation
1npm install --save js-big-decimal
Usage
:heavy_exclamation_mark: Note: Usage has changed since version 1.1.4
Require in javascript
as
1var bigDecimal = require('js-big-decimal');
For typescript
, use
1import bigDecimal = require('js-big-decimal');
For web
, when used with script tag, a variable on Window
object is created.
1<script src="node_modules/js-big-decimal/dist/web/js-big-decimal.min.js"></script>
1console.log(bigDecimal.add('12', '45'));
If you are only using it on the web and do not wish to get the whole npm package, you can download the file from unpkg as follows:
1<script src="https://unpkg.com/js-big-decimal@1.3.1/dist/web/js-big-decimal.min.js"></script>
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.
1var n1 = new bigDecimal(12.6789); 2var n2 = new bigDecimal("12345.6789"); 3var n3 = new bigDecimal('12.456e3'); // 12456
getValue()
Returns the string value of the decimal.
1console.log(n2.getValue()); // "12345.6789"
setValue()
Allows setting the BigDecimal to a new value.
1var n = new bigDecimal('123'); 2n.setValue('567'); 3console.log(n.getValue()); // 567
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.
1var value = bigDecimal.getPrettyValue("12345.6789"); // value = "12,345.6789"
Alternately, use the instance property. It returns the result as string
.
1var n3 = n2.getPrettyValue(); // n4 = "12,345.6789" 2 3var num = new bigDecimal(1234567890123456) 4var card = num.getPrettyValue(4, '-'); // cardNumber = "1234-5678-9012-3456"
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.
1var value = bigDecimal.round("123.678", 2); // value = "123.68"
Alternately, use the instance property. It returns the result as bigDecimal
.
1var n3 = n1.round(2); // n3 = new bigDecimal("12.68") 2var n4 = n2.round(); // n4 = new bigDecimal("12346")
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.
1var val1 = bigDecimal.round("123.78", -2); // val1 = "100" 2var val2 = bigDecimal.round("587", -1); // val2 = "590" 3var val3 = bigDecimal.round("123.78", -4); // val3 = "0"
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
1var num = new bigDecimal("123.657"); 2var numRound1 = num.round(1, bigDecimal.RoundingModes.DOWN); // "123.6" 3var numRound2 = num.round(2, bigDecimal.RoundingModes.CEILING); // "123.66"
stripTrailingZero(number)
Returns the number with trailing zeroes (prefix and suffix) removed.
1var n1 = bigDecimal.stripTrailingZero(300.30) // "300.3" 2var n2 = bigDecimal.stripTrailingZero(-0015.1) // "-15.1" 3var n3 = bigDecimal.stripTrailingZero(0.000) // by default defined as "0"
The instance returns the result as new bigDecimal
1var n1 = new bigDecimal(5.100).stripTrailingZero() // bigDecimal(5.1) 2var n2 = new bigDecimal(1.05).add(new bigDecimal(1.05)).stripTrailingZero() // bigDecimal(2.1)
abs(number)
Returns the absolute value of a number.
1var n1 = bigDecimal.abs(12.8) // "12.8" 2var n2 = bigDecimal.abs(-12.3) // "12.3"
The instance returns the result as new bigDecimal
1var n1 = new bigDecimal(12.8).abs() // bigDecimal(12.8) 2var n2 = new bigDecimal(-12.3).abs() // bigDecimal(-12.3)
floor(number)
Returns the whole number nearest but not greater than the input number.
1var n1 = bigDecimal.floor(12.8) // "12" 2var n2 = bigDecimal.floor(-12.3) // "-13"
The instance function returns the result as a new bigDecimal
1var n1 = new bigDecimal(12.8).floor() // bigDecimal(12) 2var n2 = bigDecimal(-12.3).floor() // bigDecimal(-13)
ceil(number)
Returns the whole number nearest but not lesser than the input number.
1var n1 = bigDecimal.ceil(12.8) // "13" 2var n2 = bigDecimal.ceil(-12.3) // "-12"
The instance function returns the result as a new bigDecimal
1var n1 = new bigDecimal(12.8).ceil() // bigDecimal(13) 2var n2 = bigDecimal(-12.3).ceil() // bigDecimal(-12)
compareTo(number1, number2)
Compare two numbers. Returns 1, 0 and -1
if number1 > number2, number1 == number2 and number1 < number2
respectively.
1var value = bigDecimal.compareTo("23.678", "67.34"); // value = -1 2var value = bigDecimal.compareTo("23.678", "23.6780"); // value = 0 3var value = bigDecimal.compareTo("123.678", "67.34"); // value = 1
Alternately, use the instance property. It returns the result as Integer
.
1var n1 = new bigDecimal('1234'); 2var n2 = new bigDecimal('8765'); 3var value = n1.compareTo(n2); // value = -1
negate(number)
Returns negation of a given number.
1var value = bigDecimal.negate("123.678"); // value = "-123.678";
Alternately, use the instance property. It returns the result as new bigDecimal
.
1var n = new bigDecimal('-1234'); 2var value = n.negate(); // value = new bigDecimal('1234')
add(augend, addend)
Add two numbers. Pass in negative for subtraction. Ensure parameters are string
s.
1var sum = bigDecimal.add("23.678", "67.34"); // sum = "91.018" 2var diff = bigDecimal.add("67.34", "-23.678"); // diff = "43.662"
Alternately, use the instance property. It returns the result as new bigDecimal
.
1var n1 = new bigDecimal('1234'); 2var n2 = new bigDecimal('8765'); 3var sum = n1.add(n2); // sum = new bigDecimal('9999')
subtract(minuend, subtrahend)
Subtract one number from another
1var diff = bigDecimal.subtract("67.34", "23.678"); // diff = "43.662"
Alternately, use the instance property. It returns the result as new bigDecimal
.
1var n1 = new bigDecimal('12.67'); 2var n2 = new bigDecimal('130.7'); 3var diff = n1.subtract(n2); // diff = new bigDecimal('-118.03')
multiply(multiplicand, multiplier)
Multiply two numbers. Ensure parameters are string
s.
1var product = bigDecimal.multiply("-0.13", "0.00130"); // product = "-0.000169"
Alternately, use the instance property. It returns the result as new bigDecimal
.
1var n1 = new bigDecimal('-0.13'); 2var n2 = new bigDecimal('0.00130'); 3var product = n1.multiply(n2); // product = new bigDecimal('-0.000169')
divide(dividend, divisor, precision, roundingMode)
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. roundingMode
has a default value of HALF_EVEN
.
1var quotient = bigDecimal.divide('45', '4', 2); // quotient = '11.25'
1var quotient = bigDecimal.divide('45', '4', 0, RoundingModes.CEILING); // quotient = '12'
Alternately, use the instance property. It returns the result as new bigDecimal
.
1var n1 = new bigDecimal('45'); 2var n2 = new bigDecimal('4'); 3var quotient = n1.divide(n2); // quotient = new bigDecimal('11.25')
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.
1var remainder = bigDecimal.modulus('45', '4'); // remainder = '1'
Alternately, use the instance property. It returns the result as new bigDecimal
.
1var n1 = new bigDecimal('45'); 2var n2 = new bigDecimal('4'); 3var remainder = n1.modulus(n2); // remainder = new bigDecimal('1')
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.
Support the developers :heart: :star: :money_with_wings:
If this library helps you in your organization, you can show some love by giving the repo a star or support by making a nominal monetary contribution.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
SAST tool detected but not run on all commits
Details
- Info: SAST configuration detected: CodeQL
- Warn: 9 commits out of 28 are checked with a SAST tool
Reason
security policy file detected
Details
- Info: security policy file detected: SECURITY.md:1
- Warn: no linked content found
- Info: Found disclosure, vulnerability, and/or timelines in security policy: SECURITY.md:1
- Info: Found text in security policy: SECURITY.md:1
Reason
2 commit(s) and 2 issue activity found in the last 90 days -- score normalized to 3
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:38: update your workflow using https://app.stepsecurity.io/secureworkflow/royNiladri/js-big-decimal/codeql-analysis.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:42: update your workflow using https://app.stepsecurity.io/secureworkflow/royNiladri/js-big-decimal/codeql-analysis.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:53: update your workflow using https://app.stepsecurity.io/secureworkflow/royNiladri/js-big-decimal/codeql-analysis.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:67: update your workflow using https://app.stepsecurity.io/secureworkflow/royNiladri/js-big-decimal/codeql-analysis.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:26: update your workflow using https://app.stepsecurity.io/secureworkflow/royNiladri/js-big-decimal/main.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:29: update your workflow using https://app.stepsecurity.io/secureworkflow/royNiladri/js-big-decimal/main.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/main.yml:47: update your workflow using https://app.stepsecurity.io/secureworkflow/royNiladri/js-big-decimal/main.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/npm-publish.yml:17: update your workflow using https://app.stepsecurity.io/secureworkflow/royNiladri/js-big-decimal/npm-publish.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/npm-publish.yml:18: update your workflow using https://app.stepsecurity.io/secureworkflow/royNiladri/js-big-decimal/npm-publish.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/npm-publish.yml:30: update your workflow using https://app.stepsecurity.io/secureworkflow/royNiladri/js-big-decimal/npm-publish.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/npm-publish.yml:31: update your workflow using https://app.stepsecurity.io/secureworkflow/royNiladri/js-big-decimal/npm-publish.yml/master?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/main.yml:35
- Info: 0 out of 10 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 third-party GitHubAction dependencies pinned
- Info: 2 out of 3 npmCommand dependencies pinned
Reason
badge detected: InProgress
Reason
Found 4/27 approved changesets -- score normalized to 1
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/codeql-analysis.yml:1
- Warn: no topLevel permission defined: .github/workflows/main.yml:1
- Warn: no topLevel permission defined: .github/workflows/npm-publish.yml:1
- Info: no jobLevel write permissions found
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
10 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-4gmj-3p3h-gm8h
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-4vvj-4cpr-p986
Score
4.2
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to js-big-decimal
big-js
A small, fast, easy-to-use library for arbitrary-precision decimal arithmetic
@fox-js/big-decimal
Fox Big Decimal utils
@gattamaneni/js-big-decimal
Work with large numbers on the client side. Round them off to any required precission.
js-big-decimal-esm
Work with large numbers on the client side. Round them off to any required precision.