ReasonJun

Hardhat : ethers.utils.parseUnits() 본문

Blockchain/HardHat

Hardhat : ethers.utils.parseUnits()

ReasonJun 2023. 10. 21. 18:07
728x90

ethers.utils.parseUnits() is a utility function provided by the ethers.js library, which is commonly used for Ethereum development. This function is used to convert a decimal number into its equivalent representation in a specific Ethereum denomination, typically in Wei.

 

In Ethereum, the smallest unit of Ether is Wei. 1 Ether is equal to 10^18 Wei. So, when working with Ether values, it's common to convert them to Wei for precision. The parseUnits() function simplifies this conversion.

 

Here's the basic usage of ethers.utils.parseUnits():

const ethers = require('ethers'); // Import the ethers.js library

// Example: Convert 1.5 Ether to Wei
const etherValue = '1.5'; // This is in Ether
const weiValue = ethers.utils.parseUnits(etherValue, 18); // 18 is the number of decimal places in Wei

console.log(weiValue.toString()); // This will output the equivalent value in Wei

In the example above, ethers.utils.parseUnits() takes the value 1.5 and converts it to its equivalent in Wei, considering 18 decimal places. The result is then represented as a BigNumber in ethers.js.

 

This function is useful when working with Ethereum values, especially in smart contract development, where precise conversions between different denominations (Wei, Gwei, Ether, etc.) are often required.

728x90
Comments