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.
AUnit can be specified as a number, which indicates the number of decimal places that should be used.
Examples:
There are also several commonNamed Units, in which case their name (as a string) may be used.
Name | Decimals | |
wei | 0 | |
kwei | 3 | |
mwei | 6 | |
gwei | 9 | |
szabo | 12 | |
finney | 15 | |
ether | 18 |
Returns a string with value grouped by 3 digits, separated by,
.
Returns a string representation ofvalue formatted withunit digits (if it is a number) or to the unit specified (if a string).
The equivalent to callingformatUnits(value, "ether")
.
Returns aBigNumber representation ofvalue, parsed withunit digits (if it is a number) or from the unit specified (if a string).