What is @ethersproject/bignumber?
@ethersproject/bignumber is a library for handling large numbers in JavaScript, particularly useful in the context of Ethereum and blockchain development. It provides utilities for creating, manipulating, and performing arithmetic operations on big numbers, which are common in blockchain transactions and smart contracts.
What are @ethersproject/bignumber's main functionalities?
Creating Big Numbers
This feature allows you to create big numbers from strings, numbers, or hex values. The example demonstrates creating a BigNumber instance from a string and converting it back to a string for display.
const { BigNumber } = require('@ethersproject/bignumber');
const bigNumber = BigNumber.from('12345678901234567890');
console.log(bigNumber.toString());
Arithmetic Operations
This feature provides methods for performing arithmetic operations such as addition, subtraction, multiplication, and division on BigNumber instances. The example shows how to add two BigNumber instances.
const { BigNumber } = require('@ethersproject/bignumber');
const a = BigNumber.from(10);
const b = BigNumber.from(20);
const sum = a.add(b);
console.log(sum.toString());
Comparison Operations
This feature includes methods for comparing BigNumber instances, such as less than (lt), greater than (gt), and equals (eq). The example demonstrates comparing two BigNumber instances.
const { BigNumber } = require('@ethersproject/bignumber');
const a = BigNumber.from(10);
const b = BigNumber.from(20);
console.log(a.lt(b)); // true
console.log(a.eq(b)); // false
Formatting and Parsing
This feature allows you to format BigNumber instances to different string representations, such as hexadecimal. The example shows how to convert a BigNumber to a hex string.
const { BigNumber } = require('@ethersproject/bignumber');
const bigNumber = BigNumber.from('12345678901234567890');
const formatted = bigNumber.toHexString();
console.log(formatted);
Other packages similar to @ethersproject/bignumber
bignumber.js
bignumber.js is a popular library for arbitrary-precision decimal and non-decimal arithmetic in JavaScript. It provides similar functionality to @ethersproject/bignumber but is more general-purpose and not specifically tailored for Ethereum or blockchain development.
bn.js
bn.js is a library for arbitrary-precision arithmetic in JavaScript. It is widely used in cryptographic applications and provides a comprehensive set of methods for working with large integers. Compared to @ethersproject/bignumber, bn.js is more focused on performance and low-level operations.
decimal.js
decimal.js is a library for arbitrary-precision decimal arithmetic in JavaScript. It is similar to bignumber.js but focuses on decimal numbers rather than integers. It provides a rich set of methods for mathematical operations and is suitable for financial calculations.
Big Numbers
This sub-module is part of the ethers project.
It is responsible for handling arbitrarily large numbers and mathematic operations.
For more information, see the documentation for Big Numbers
and Fixed-Point Numbers.
Importing
Most users will prefer to use the umbrella package,
but for those with more specific needs, individual components can be imported.
const {
BigNumber,
FixedFormat,
FixedNumber,
formatFixed,
parseFixed
BigNumberish
} = require("@ethersproject/bignumber");
License
MIT License