What is @ethersproject/units?
@ethersproject/units is a utility library for handling and converting between different units of Ether and other cryptocurrencies. It provides functions to parse and format units, making it easier to work with values in different denominations.
What are @ethersproject/units's main functionalities?
parseUnits
The `parseUnits` function converts a string representation of a value in a specified unit to its equivalent in wei (the smallest unit of Ether). In this example, '1.0' Ether is converted to '1000000000000000000' wei.
const { parseUnits } = require('@ethersproject/units');
const weiValue = parseUnits('1.0', 'ether');
console.log(weiValue.toString()); // '1000000000000000000'
formatUnits
The `formatUnits` function converts a value in wei to a string representation in a specified unit. In this example, '1000000000000000000' wei is converted to '1.0' Ether.
const { formatUnits } = require('@ethersproject/units');
const etherValue = formatUnits('1000000000000000000', 'ether');
console.log(etherValue); // '1.0'
commify
The `commify` function adds commas to a numeric string for better readability. In this example, '1234567.890123' is formatted to '1,234,567.890123'.
const { commify } = require('@ethersproject/units');
const formattedValue = commify('1234567.890123');
console.log(formattedValue); // '1,234,567.890123'
Other packages similar to @ethersproject/units
web3-utils
The `web3-utils` package is part of the Web3.js library and provides utility functions for Ethereum dapp development. It includes functions for unit conversion, such as `toWei` and `fromWei`, which are similar to `parseUnits` and `formatUnits` in @ethersproject/units. However, `web3-utils` is more tightly integrated with the Web3.js ecosystem.
bignumber.js
The `bignumber.js` package is a library for arbitrary-precision decimal and non-decimal arithmetic. While it does not specifically focus on Ethereum unit conversions, it can be used in conjunction with other libraries to handle large numbers and perform precise calculations, similar to the functionality provided by @ethersproject/units.
ethers
The `ethers` package is a complete Ethereum library that includes the `@ethersproject/units` module as part of its functionality. It provides a comprehensive set of tools for interacting with the Ethereum blockchain, including unit conversion, contract interaction, and wallet management. It is more feature-rich compared to @ethersproject/units alone.
Ethereum Unit Conversion Utilities
This sub-module is part of the ethers project.
It contains functions to convert between string representations and numeric
representations of numbers, including those out of the range of JavaScript.
For more information, see the documentation.
Importing
Most users will prefer to use the umbrella package,
but for those with more specific needs, individual components can be imported.
const {
formatUnits,
parseUnits,
formatEther,
parseEther,
commify
} = require("@ethersproject/units");
License
MIT License