Top Related Projects
An extensive math library for JavaScript and Node.js
An arbitrary-precision Decimal type for JavaScript
A JavaScript library for arbitrary-precision decimal and non-decimal arithmetic
An arbitrary length integer library for Javascript
Quick Overview
Big.js is a small, fast JavaScript library for arbitrary-precision decimal arithmetic. It supports operations on numbers of any size and precision, making it ideal for financial calculations and other scenarios where precision is crucial.
Pros
- High precision: Supports arbitrary-precision decimal arithmetic
- Lightweight: Small file size and minimal dependencies
- Configurable: Allows customization of rounding mode, decimal places, and more
- Well-documented: Comprehensive API documentation and examples
Cons
- Limited features: Focuses solely on decimal arithmetic, lacking advanced mathematical functions
- Performance: May be slower than native JavaScript operations for simple calculations
- Learning curve: Requires understanding of how to work with Big numbers for optimal usage
- Not suitable for all use cases: Overkill for simple calculations where native JavaScript is sufficient
Code Examples
Creating and using Big numbers:
const Big = require('big.js');
const x = new Big(0.1);
const y = new Big(0.2);
const sum = x.plus(y);
console.log(sum.toString()); // "0.3"
Performing calculations with high precision:
const Big = require('big.js');
const result = new Big(1)
.div(3)
.times(3);
console.log(result.eq(1)); // true
console.log(1/3 * 3 === 1); // false (native JavaScript)
Configuring decimal places and rounding:
const Big = require('big.js');
Big.DP = 20; // Set decimal places to 20
Big.RM = Big.roundDown; // Set rounding mode to round down
const x = new Big(1).div(3);
console.log(x.toString()); // "0.33333333333333333333"
Getting Started
To use Big.js in your project, follow these steps:
-
Install the library using npm:
npm install big.js
-
Import the library in your JavaScript file:
const Big = require('big.js');
-
Create Big numbers and perform calculations:
const x = new Big(10); const y = new Big(3); const result = x.div(y); console.log(result.toString()); // "3.3333333333333333333"
Competitor Comparisons
An extensive math library for JavaScript and Node.js
Pros of mathjs
- Comprehensive library with a wide range of mathematical functions and operations
- Supports complex numbers, matrices, and units
- Extensible with custom functions and data types
Cons of mathjs
- Larger file size and potentially slower performance for basic arithmetic operations
- Steeper learning curve due to its extensive feature set
- May be overkill for projects that only require basic arbitrary-precision arithmetic
Code Comparison
mathjs:
import { create, all } from 'mathjs'
const math = create(all)
const result = math.evaluate('1.1 + 2.2')
console.log(result.toString()) // Outputs: 3.3
big.js:
import Big from 'big.js'
const result = new Big('1.1').plus('2.2')
console.log(result.toString()) // Outputs: 3.3
Both libraries provide accurate decimal arithmetic, but mathjs offers a more extensive set of features at the cost of increased complexity and file size. big.js is more focused on arbitrary-precision decimal arithmetic and may be preferable for simpler use cases or when performance is a primary concern.
An arbitrary-precision Decimal type for JavaScript
Pros of decimal.js
- More precise and supports larger numbers (up to 1e+9999999)
- Offers additional mathematical functions (e.g., trigonometric, exponential)
- Configurable rounding modes and precision settings
Cons of decimal.js
- Larger file size and potentially slower performance
- More complex API, which may be overkill for simpler use cases
- Higher learning curve due to additional features and options
Code Comparison
decimal.js:
import Decimal from 'decimal.js';
const result = new Decimal(0.1).plus(0.2).equals(0.3); // true
const pi = Decimal.acos(-1);
big.js:
import Big from 'big.js';
const result = new Big(0.1).plus(0.2).eq(0.3); // true
// No built-in trigonometric functions
Both libraries provide accurate decimal arithmetic, but decimal.js offers more advanced mathematical operations and greater precision at the cost of increased complexity and file size. big.js is simpler and more lightweight, making it suitable for basic decimal calculations, while decimal.js is better suited for complex mathematical operations and scenarios requiring extreme precision.
A JavaScript library for arbitrary-precision decimal and non-decimal arithmetic
Pros of bignumber.js
- More comprehensive API with additional mathematical operations
- Configurable decimal places and rounding modes
- Better performance for certain operations
Cons of bignumber.js
- Larger file size compared to big.js
- Slightly more complex API, which may be overkill for simpler use cases
Code Comparison
bignumber.js:
const x = new BigNumber(0.1);
const y = new BigNumber(0.2);
const sum = x.plus(y);
console.log(sum.toString()); // "0.3"
big.js:
const x = new Big(0.1);
const y = new Big(0.2);
const sum = x.plus(y);
console.log(sum.toString()); // "0.3"
Both libraries provide similar basic functionality for handling arbitrary-precision decimal arithmetic. bignumber.js offers more advanced features and configuration options, making it suitable for complex mathematical operations. big.js, on the other hand, is more lightweight and straightforward, ideal for simpler use cases where basic arithmetic operations are sufficient.
The choice between the two depends on the specific requirements of your project, such as the complexity of calculations, performance needs, and file size constraints.
An arbitrary length integer library for Javascript
Pros of BigInteger.js
- Specialized for integer operations, potentially offering better performance for large integer calculations
- Supports a wider range of integer-specific operations, such as bitwise operations and modular exponentiation
- Smaller file size, which may lead to faster load times in web applications
Cons of BigInteger.js
- Limited to integer operations, lacking support for decimal numbers and floating-point arithmetic
- Less actively maintained, with fewer recent updates compared to big.js
- Smaller community and fewer resources available for support and troubleshooting
Code Comparison
big.js:
const x = new Big(123.4567);
const y = new Big('9876.5432');
const result = x.plus(y).toFixed(2);
BigInteger.js:
const x = BigInteger(123);
const y = BigInteger('9876');
const result = x.add(y).toString();
The code examples highlight the key difference in focus between the two libraries. big.js handles decimal numbers and provides methods for precise decimal arithmetic, while BigInteger.js is designed specifically for integer operations.
Both libraries offer similar ease of use, but big.js provides more flexibility for general-purpose calculations involving both integers and decimals. BigInteger.js, on the other hand, excels in scenarios requiring large integer computations and bitwise operations.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
big.js
A small, fast JavaScript library for arbitrary-precision decimal arithmetic.
Features
- Simple API
- Faster, smaller and easier-to-use than JavaScript versions of Java's BigDecimal
- Only 6 KB minified
- Replicates the
toExponential
,toFixed
andtoPrecision
methods of JavaScript Numbers - Stores values in an accessible decimal floating point format
- Comprehensive documentation and test set
- No dependencies
- Uses ECMAScript 3 only, so works in all browsers
The little sister to bignumber.js and decimal.js. See here for some notes on the difference between them.
Install
The library is the single JavaScript file big.js or the ES module big.mjs.
Browsers
Add Big to global scope:
<script src='path/to/big.js'></script>
ES module:
<script type='module'>
import Big from './path/to/big.mjs';
</script>
Get a minified version from a CDN:
<script src='https://cdn.jsdelivr.net/npm/big.js@7.0.1/big.min.js'></script>
Node.js
$ npm install big.js
CommonJS:
const Big = require('big.js');
ES module:
import Big from 'big.js';
Deno
import Big from 'https://raw.githubusercontent.com/mikemcl/big.js/v7.0.1/big.mjs';
import Big from 'https://unpkg.com/big.js@latest/big.mjs';
Use
In the code examples below, semicolons and toString
calls are not shown.
The library exports a single constructor function, Big
.
A Big number is created from a primitive number, string, or other Big number.
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
In Big strict mode, creating a Big number from a primitive number is disallowed.
Big.strict = true
x = new Big(1) // TypeError: [big.js] Invalid number
y = new Big('1.0000000000000001')
y.toNumber() // Error: [big.js] Imprecise conversion
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 arithmetic methods always return the exact result except div
, sqrt
and pow
(with negative exponent), as these methods involve division.
The maximum number of decimal places and the rounding mode used to round the results of these methods is determined by the value of the DP
and RM
properties of the Big
number constructor.
Big.DP = 10
Big.RM = Big.roundHalfUp
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 advanced usage, multiple Big number constructors can be created, each with an independent configuration.
For further information see the API reference documentation.
Minify
To minify using, for example, npm and terser
$ npm install -g terser
$ terser big.js -c -m -o big.min.js
Test
The test directory contains the test scripts for each Big number method.
The tests can be run with Node.js or a browser.
Run all the tests:
$ npm test
Test a single method:
$ node test/toFixed
For the browser, see runner.html and test.html in the test/browser directory.
big-vs-number.html is a old application that enables some of the methods of big.js to be compared with those of JavaScript's Number type.
TypeScript
The DefinitelyTyped project has a Typescript type definitions file for big.js.
$ npm install --save-dev @types/big.js
Any questions about the TypeScript type definitions file should be addressed to the DefinitelyTyped project.
Licence
Contributors
Financial supporters
Thank you to all who have supported this project via Open Collective, particularly Coinbase.
Top Related Projects
An extensive math library for JavaScript and Node.js
An arbitrary-precision Decimal type for JavaScript
A JavaScript library for arbitrary-precision decimal and non-decimal arithmetic
An arbitrary length integer library for Javascript
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot