Display Logic and Input
When creating an Application, it is useful to convert between user-friendly strings (usually displaying hbar) and the machine-readable values that contracts and maths depend on (usually in tinybar).
For example, an Account may specify the balance in hbar, but when sending a transaction, it must be specified in tinybar.
The parseUnits will parse a string representing hbar, such as 1.1
into a BigNumber in tinybar, and is useful when a user types in a value, such as sending 1.1 hbar.
The formatUnits will format a BigNumberish into a string, which is useful when displaying a balance.
Units
Decimal Count
A Unit can be specified as a number, which indicates the number of decimal places that should be used.
Examples:
1 hbar in tinybar, has 8 decimal places (i.e. 1 hbar represents 10^8 tinybar)
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)
Named Units
There are also several common Named Units, in which case their name (as a string) may be used.
Functions
Formatting
hethers.utils.commify( value ) ⇒ string
hethers.utils.commify( value ) ⇒ string
Returns a string with value grouped by 3 digits, separated by ,
.
Conversion
hethers.utils.formatUnits( value [ , unit = "hbar" ] ) ⇒ string
hethers.utils.formatUnits( value [ , unit = "hbar" ] ) ⇒ string
Returns a string representation of value formatted with unit digits (if it is a number) or to the unit specified (if a string).
The functions
formatEther and
parseEther have been replaced with
formatHbar and
parseHbar.
hethers.utils.formatHbar( value ) ⇒ string
hethers.utils.formatHbar( value ) ⇒ string
The equivalent to calling formatUnits(value, "hbar")
.
Returns a BigNumber representation of value, parsed with unit digits (if it is a number) or from the unit specified (if a string).
The equivalent to calling parseUnits(value, "hbar")
.
Last updated