web3-bignumber
v0.1.2
Published
Web3 BigNumber is an extensive library for managing large number for Javascript and Node.js. Useful for web3 development.
Downloads
13
Maintainers
Readme
Web3 BigNumber
Web3 BigNumber is an extensive library for managing large number for Javascript and Node.js. Useful for web3 development.
Installation
You can install the package using NPM
Using NPM
npm install web3-bignumber
Using Yarn
yarn add web3-bignumber
Getting Started
- :writing_hand: If you have questions submit an issue
Prerequisites
Usage
Import
const { Web3BigNumber } = require("web3-bignumber");
OR
import { Web3BigNumber } from "web3-bignumber";
Web3BigNumber(number: bigint | string | number)
Basic Usage
Simple Formatting
Web3BigNumber(1e+27).value();
// Output: 1000000000000000000000000000
From small to big
Web3BigNumber(1000).toBig(18);
// Output: 1000000000000000000000
From big to small
Web3BigNumber("12300000000000000").toSmall(18);
// Output: 0.0123
Arithmatic Operations
Addition
Web3BigNumber("100").add(50);
// Output: 150
Subtract
Web3BigNumber("100").subtract(50);
// Output: 50
Multiply
Web3BigNumber("100").multiply(5);
// Output: 500
Division
Web3BigNumber("50").divide(5);
// Output: 10
Manipulate
To Big Integer
Web3BigNumber("1234500000000000").toBigInt();
// Output: 1234500000000000n
To Integer
Web3BigNumber("123").toInteger();
// Output: 123
Comma Seperated
Web3BigNumber("1234500000000").toCommaSeparated();
// Output: 1,234,500,000,000
Compact Number
Web3BigNumber("1000000").toCompactNumber();
// Output: 1M
Percentage
Web3BigNumber("0.123").toPercent();
// Output: 12.3%
Fixed Decimal Places
Web3BigNumber("12.34567").trimDecimalPlaces(2);
// Output: 12.35
Conditional Check
Check Zero
Web3BigNumber(0).isZero();
// Output: true
Web3BigNumber(1).isZero();
// Output: false
Check Negative
Web3BigNumber(-123).isNegative();
// Output: true
Web3BigNumber(123).isNegative();
// Output: false
Equal
Web3BigNumber(123).equals("123");
// Output: true
Web3BigNumber(123).equals(456);
// Output: false
Greater Than
Web3BigNumber(123).gt("5");
// Output: true
Web3BigNumber(123).gt(456);
// Output: false
Greater Than or Equal
Web3BigNumber(123).gte("123");
// Output: true
Web3BigNumber(123).gte(456);
// Output: false
Less Than
Web3BigNumber(123).lt("1234");
// Output: true
Web3BigNumber(123).lt(5);
// Output: false
Less Than or Equal
Web3BigNumber(123).lte("123");
// Output: true
Web3BigNumber(123).lte(5);
// Output: false
Check Maximum
Web3BigNumber(123).max("1234");
// Output: 1234
Check Minimum
Web3BigNumber(123).min("1234");
// Output: 123
Prototype Functions
// Example
Web3BigNumber(1000).subtract("1000").isZero();
// Output: true
All Prototype Functions
String | Number | BigInt
toBigInt()
toCommaSeparated()
toCompactNumber()
toPercent()
toInteger()
isZero()
isNegative()
equals(num2: number | string | bigint)
gt(num2: number | string | bigint)
gte(num2: number | string | bigint)
lt(num2: number | string | bigint)
lte(num2: number | string | bigint)
add(num2: number | string | bigint)
subtract(num2: number | string | bigint)
multiply(num2: number | string | bigint)
divide(num2: number | string | bigint)
max(num2: number | string | bigint)
min(num2: number | string | bigint)