Movatterモバイル変換


[0]ホーム

URL:


ethers
v5.7
Documentation
Getting Started
Ethereum Basics
Events
Gas
Security
Best Practices
Provider API Keys
Application Programming Interface
Providers
Provider
JsonRpcProvider
API Providers
Other Providers
Types
Signers
Contract Interaction
Contract
ContractFactory
Example: ERC-20 Contract
Utilities
Application Binary Interface
AbiCoder
ABI Formats
Fragments
Interface
Addresses
BigNumber
Byte Manipulation
Constants
Display Logic and Input
Units
Functions
Encoding Utilities
FixedNumber
Hashing Algorithms
HD Wallet
Logging
Property Utilities
Signing Key
Strings
Transactions
Web Utilities
Wordlists
Other Libraries
Assembly
Ethers ASM Dialect
Utilities
Abstract Syntax Tree
Hardware Wallets
Experimental
Command Line Interfaces
Sandbox Utility
Assembler
Ethereum Naming Service
TypeScript
Making Your Own
Cookbook
React Native (and ilk)
Transactions
Migration Guide
Migration: From Web3.js
Migration: From Ethers v4
Testing
Contributing and Hacking
Other Resources
Flatworm Docs
License and Copyright
Single Page
Documentation  »  API  »  Utilities  »  Display Logic and Input

Display Logic and Input

When creating an Application, it is useful to convert between user-friendly strings (usually displayingether) and the machine-readable values that contracts and maths depend on (usually inwei).

For example, a Wallet may specify the balance in ether, and gas prices in gwei for the User Interface, but when sending a transaction, both must be specified in wei.

TheparseUnits will parse a string representing ether, such as1.1 into aBigNumber in wei, and is useful when a user types in a value, such as sending 1.1 ether.

TheformatUnits will format aBigNumberish into a string, which is useful when displaying a balance.

Units

Decimal Count

AUnit can be specified as a number, which indicates the number of decimal places that should be used.

Examples:

Named Units

There are also several commonNamed Units, in which case their name (as a string) may be used.

NameDecimals 
wei0 
kwei3 
mwei6 
gwei9 
szabo12 
finney15 
ether18 

Functions

Formatting

ethers.utils.commify(value)string
source

Returns a string with value grouped by 3 digits, separated by,.

commify("-1000.3000");// '-1,000.3'

Conversion

ethers.utils.formatUnits(value[,unit = "ether"])string
source

Returns a string representation ofvalue formatted withunit digits (if it is a number) or to the unit specified (if a string).

const oneGwei = BigNumber.from("1000000000");const oneEther = BigNumber.from("1000000000000000000");formatUnits(oneGwei, 0);// '1000000000'formatUnits(oneGwei, "gwei");// '1.0'formatUnits(oneGwei, 9);// '1.0'formatUnits(oneEther);// '1.0'formatUnits(oneEther, 18);// '1.0'
ethers.utils.formatEther(value)string
source

The equivalent to callingformatUnits(value, "ether").

const value = BigNumber.from("1000000000000000000");formatEther(value);// '1.0'
ethers.utils.parseUnits(value[,unit = "ether"])BigNumber
source

Returns aBigNumber representation ofvalue, parsed withunit digits (if it is a number) or from the unit specified (if a string).

parseUnits("1.0");// { BigNumber: "1000000000000000000" }parseUnits("1.0", "ether");// { BigNumber: "1000000000000000000" }parseUnits("1.0", 18);// { BigNumber: "1000000000000000000" }parseUnits("121.0", "gwei");// { BigNumber: "121000000000" }parseUnits("121.0", 9);// { BigNumber: "121000000000" }
ethers.utils.parseEther(value)BigNumber
source

The equivalent to callingparseUnits(value, "ether").

parseEther("1.0");// { BigNumber: "1000000000000000000" }parseEther("-0.5");// { BigNumber: "-500000000000000000" }
Constants
Encoding Utilities
The content of this site is licensed under theCreative Commons License. Generated on April 6, 2023, 1:54am.

[8]ページ先頭

©2009-2025 Movatter.jp