2019-08-21 08:53:47 +03:00
|
|
|
_section: Display Logic and Input
|
|
|
|
|
|
|
|
When creating an Application, it is useful to convert between
|
|
|
|
user-friendly strings (usually displaying **ether**) and the
|
|
|
|
machine-readable values that contracts and maths depend on
|
|
|
|
(usually in **wei**).
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
The [parseUnits](unit-conversion) will parse a string representing
|
|
|
|
ether, such as ``1.1`` into a [BigNumber](bignumber) in wei, and is
|
|
|
|
useful when a user types in a value, such as sending 1.1 ether.
|
|
|
|
|
|
|
|
The [formatUnits](unit-conversion) will format a [BigNumberish](bignumberish)
|
|
|
|
into a string, which is useful when displaying a balance.
|
|
|
|
|
|
|
|
|
|
|
|
_subsection: Units
|
|
|
|
|
|
|
|
_heading: Decimal Count
|
|
|
|
|
2020-04-19 09:18:20 +03:00
|
|
|
A **Unit** can be specified as an number, which indicates the
|
|
|
|
number of decimal places that should be used.
|
|
|
|
|
|
|
|
**Examples:**
|
|
|
|
|
|
|
|
- 1 ether in wei, has **18** decimal places (i.e. 1 ether represents 10^^18^^ wei)
|
|
|
|
- 1 bitcoin in Satoshi, has **8** decimal places (i.e. 1 bitcoin represents 10^^8^^ satoshi)
|
2019-08-21 08:53:47 +03:00
|
|
|
|
|
|
|
_heading: Named Units
|
|
|
|
|
2020-04-19 09:18:20 +03:00
|
|
|
There are also several common **Named Units**, in which case their name (as
|
|
|
|
a string) may be used.
|
|
|
|
|
|
|
|
_table: @STYLE<compact>
|
2019-08-21 08:53:47 +03:00
|
|
|
|
2020-04-19 09:18:20 +03:00
|
|
|
| **Name** | **Decimals** |
|
|
|
|
| //wei// | 0 |
|
|
|
|
| //kwei// | 3 |
|
|
|
|
| //mwei// | 6 |
|
|
|
|
| //gwei// | 9 |
|
|
|
|
| //szabo// | 12 |
|
|
|
|
| //finney// | 15 |
|
|
|
|
| //ether// | 18 |
|
2019-08-21 08:53:47 +03:00
|
|
|
|
|
|
|
|
|
|
|
_subsection: Functions
|
|
|
|
|
|
|
|
_heading: Formatting
|
|
|
|
|
2020-02-25 22:57:11 +03:00
|
|
|
_property: ethers.utils.commify(value) => string @<utils-commify> @SRC<units>
|
2019-08-21 08:53:47 +03:00
|
|
|
Returns a string with value grouped by 3 digits, separated by ``,``.
|
|
|
|
|
|
|
|
|
|
|
|
_heading: Conversion @<unit-conversion>
|
|
|
|
|
2020-02-25 22:57:11 +03:00
|
|
|
_property: ethers.utils.formatUnits(value [ , unit = "ether" ] ) => string @<utils-formatunits> @SRC<units>
|
2019-08-21 08:53:47 +03:00
|
|
|
Returns a string representation of //value// formatted with //unit//
|
|
|
|
digits (if it is a number) or to the unit specified (if a string).
|
|
|
|
|
2020-02-25 22:57:11 +03:00
|
|
|
_property: ethers.utils.formatEther(value) => string @<utils-formatether> @SRC<units>
|
2019-08-21 08:53:47 +03:00
|
|
|
The equivalent to calling ``formatUnits(value, "ether")``.
|
|
|
|
|
2020-02-25 22:57:11 +03:00
|
|
|
_property: ethers.utils.parseUnits(value [ , unit = "ether" ] ) => [BigNumber](bignumber) @<utils-parseunits> @SRC<units>
|
2019-08-21 08:53:47 +03:00
|
|
|
Returns a [BigNumber](bignumber) representation of //value//, parsed with
|
|
|
|
//unit// digits (if it is a number) or from the unit specified (if
|
|
|
|
a string).
|
|
|
|
|
2020-02-25 22:57:11 +03:00
|
|
|
_property: ethers.utils.parseEther(value) => [BigNumber](bignumber) @<utils-parseether> @SRC<units>
|
2019-08-21 08:53:47 +03:00
|
|
|
The equivalent to calling ``parseUnits(value, "ether")``.
|