Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
The big.js npm package is a library for arbitrary-precision decimal arithmetic. It allows you to perform calculations on numbers with a large number of digits without losing precision, which is a common issue with JavaScript's native Number type.
Arithmetic Operations
Perform precise addition, subtraction, multiplication, and division operations.
{"addition": "new Big('0.1').plus(new Big('0.2')).toString()", "subtraction": "new Big('0.3').minus(new Big('0.1')).toString()", "multiplication": "new Big('0.2').times(new Big('0.3')).toString()", "division": "new Big('0.3').div(new Big('0.2')).toString()"}
Comparison Operations
Compare two Big numbers to determine equality, or whether one is greater than or less than the other.
{"equals": "new Big('1.0').eq(new Big('1.00'))", "greaterThan": "new Big('2').gt(new Big('1.9999'))", "lessThan": "new Big('0.1').lt(new Big('0.2'))"}
Rounding
Round Big numbers to a specified number of decimal places, or convert a Big number to a fixed-point notation string.
{"round": "new Big('0.12345').round(2).toString()", "toFixed": "new Big('0.12345').toFixed(2)"}
Configuration
Configure the number of decimal places for rounding and the rounding mode (e.g., round half up, round down, etc.).
{"setDP": "Big.DP = 10", "setRM": "Big.RM = 1"}
bignumber.js is another arbitrary-precision decimal and non-decimal arithmetic library with similar functionality to big.js. It provides more features, such as support for non-decimal bases, but it might be slower for some operations due to its broader scope.
decimal.js is a library for arbitrary-precision arithmetic that is similar to big.js but with additional features like trigonometric and logarithmic functions, which big.js does not have. It also allows for immutable Decimal instances.
fraction.js is focused on rational numbers (fractions) and provides arithmetic operations for them. It is different from big.js, which deals with decimal numbers, but it can be used for high-precision calculations where representing numbers as fractions is more appropriate.
A small, fast JavaScript library for arbitrary-precision decimal arithmetic.
The little sister to bignumber.js. See also decimal.js, and here for the difference between them.
toExponential
, toFixed
and toPrecision
methods of JavaScript's Number typesqrt
methodThe library is the single JavaScript file big.js (or big.min.js, which is big.js minified).
It can be loaded via a script tag in an HTML document for the browser
<script src='./relative/path/to/big.js'></script>
or as a CommonJS, Node.js or AMD module using require
.
var Big = require('big.js');
For Node.js, the library is available from the npm registry:
$ npm install big.js
In all examples below, var
, semicolons and toString
calls are not shown.
If a commented-out value is in quotes it means toString
has been called on the preceding expression.
The library exports a single function: Big, the constructor of Big number instances. It accepts a value of type Number, String or Big number Object.
x = new Big(123.4567)
y = Big('123456.7e-3') // 'new' is optional
z = new Big(x)
x.eq(y) && x.eq(z) && y.eq(z) // true
A Big number is immutable in the sense that it is not changed by its methods.
0.3 - 0.1 // 0.19999999999999998
x = new Big(0.3)
x.minus(0.1) // "0.2"
x // "0.3"
The methods that return a Big number can be chained.
x.div(y).plus(z).times(9).minus('1.234567801234567e+8').plus(976.54321).div('2598.11772')
x.sqrt().div(y).pow(3).gt(y.mod(z)) // true
Like JavaScript's Number type, there are toExponential
, toFixed
and toPrecision
methods.
x = new Big(255.5)
x.toExponential(5) // "2.55500e+2"
x.toFixed(5) // "255.50000"
x.toPrecision(5) // "255.50"
The maximum number of decimal places and the rounding mode used to round the results of the div
, sqrt
and pow
(with negative exponent) methods is determined by the value of the DP
and RM
properties of the Big
number constructor.
The other methods always give the exact result.
(From v3.0.0, multiple Big number constructors can be created, see Change Log below.)
Big.DP = 10
Big.RM = 1
x = new Big(2);
y = new Big(3);
z = x.div(y) // "0.6666666667"
z.sqrt() // "0.8164965809"
z.pow(-3) // "3.3749999995"
z.times(z) // "0.44444444448888888889"
z.times(z).round(10) // "0.4444444445"
The value of a Big number is stored in a decimal floating point format in terms of a coefficient, exponent and sign.
x = new Big(-123.456);
x.c // [1,2,3,4,5,6] coefficient (i.e. significand)
x.e // 2 exponent
x.s // -1 sign
For further information see the API reference from the doc folder.
The test directory contains the test scripts for each Big number method.
The tests can be run with Node or a browser.
To test a single method, from a command-line shell at the test directory, use e.g.
$ node toFixed
To test all the methods
$ node every-test
For the browser, see single-test.html and every-test.html in the test/browser directory.
big-vs-number.html enables some of the methods of big.js to be compared with those of JavaScript's Number type.
The perf directory contains two applications and a lib directory containing the BigDecimal libraries used by both.
big-vs-bigdecimal.html tests the performance of big.js against the JavaScript translations of two versions of BigDecimal, its use should be more or less self-explanatory. (The GWT version doesn't work in IE 6.)
The BigDecimal in Node's npm registry is the GWT version. Despite its seeming popularity I have found it to have some serious bugs, see the Node script perf/lib/bigdecimal_GWT/bugs.js for examples of flaws in its remainder, divide and compareTo methods.
bigtime.js is a Node command-line application which tests the performance of big.js against the GWT version of BigDecimal from the npm registry.
For example, to compare the time taken by the big.js plus
method and the BigDecimal add
method:
$ node bigtime plus 10000 40
This will time 10000 calls to each, using operands of up to 40 random digits and will check that the results match.
For help:
$ node bigtime -h
I.e. minify.
For Node, if uglify-js is installed globally ( npm install uglify-js -g
) then
uglifyjs -o ./big.min.js ./big.js
will create big.min.js.
The DefinitelyTyped project has a TypeScript definitions file for big.js.
The definitions file can be added to your project via the big.js.TypeScript.DefinitelyTyped NuGet package or via tsd.
tsd query big.js --action install
Any questions about the TypeScript definitions file should be addressed to the DefinitelyTyped project.
Feedback is welcome.
Bugs/comments/questions? Open an issue, or email
Michael M8ch88l@gmail.com
See LICENCE.
####5.0.1
Big.version
number.####5.0.0
-0
from valueOf
for negative zero.Big.version
.####4.0.2
--experimental-modules
flag.####4.0.0
Big.E_POS
to Big.PE
, Big.E_NEG
to Big.NE
.null
is passed to toFixed
etc.####3.2.0
####3.1.3
####3.1.2
####3.1.1
####3.1.0
TO_EXP_NEG
and TO_EXP_POS
as Big.E_NEG
and Big.E_POS
.####3.0.2
files
field in package.json instead.####3.0.1
sub
, add
and mul
aliases.####3.0.0
####2.5.2
####2.5.1
####2.5.0
toJSON
method so serialization uses toString
.####2.4.1
####2.4.0
Error
.####2.3.0
cmp
method.####2.2.0
####2.1.0
.1
and 2.
.####2.0.0
abs
method and replaced cmp
with eq
, gt
, gte
, lt
, and lte
methods.####1.0.1
####1.0.0
FAQs
A small, fast, easy-to-use library for arbitrary-precision decimal arithmetic
The npm package big.js receives a total of 17,517,593 weekly downloads. As such, big.js popularity was classified as popular.
We found that big.js demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.