The application needs to display amounts of different sizes, from extremely small to extremely large, while maintaining accuracy.
There are two types of amounts:
- token amount:
100 000.54 COW
- fiat amounts:
≈ $1 946 628.4
localStorage.setItem('amountsRefactoring', '1')
- to highlight components with formatted tokens and fiat amounts
The solution uses Intl.NumberFormat API for numbers formatting.
All functions and components mentioned in this documents are using trimTrailingZeros()
function to avoid values with trailing zeros.
Example: trimTrailingZeros('12.092000') -> 12.092
Example: trimTrailingZeros('24.000000') -> 24
The smart
precision algorithm:
- When an amount is less than
1
, then precision is6
(0.003411
) - When an amount is less than
100_000
, then precision is4
(2 002.5901
) - When an amount is less than
1M
, then precision is3
(640 123.012
) - When an amount is less than
10M
, then precision is2
(2 700 000.08
) - When an amount is greater than
10M
, then precision is3
(65 200 000.102
) - When an amount is greater than
1B
, then precision is3
AND plus suffixB
(12.502B
) - When an amount is greater than
1T
, then precision is3
AND plus suffixT
adds (6.2T
)
Examples:
<TokenAmount amount={amount} tokenSymbol={amount.currency} />
- looks like 400 DAI
<TokenAmount amount={amount} />
- looks like 6 000.11
The component has title
attribute and displays the full amount on hover.
The amount
property has type FractionLike
. It includes Fraction
, Price
and CurrencyAmount
from @uniswap/sdk-core
An amount could be formatted using formatTokenAmount(amount)
function.
Or formatAmountWithPrecision(amount, precision)
if you need custom precision.
Examples:
<FiatAmount amount={amount}
- looks like ≈ $2 001.54
The precision for fiat amounts is always 2