diff options
author | Brandon Millman <brandon.millman@gmail.com> | 2018-07-03 07:00:34 +0800 |
---|---|---|
committer | Brandon Millman <brandon.millman@gmail.com> | 2018-07-03 07:08:02 +0800 |
commit | bc7e8ff471d7abe8e65b30d1945f6a989d521f10 (patch) | |
tree | c644cd1c61783b0394534c7cd47fd28637ec46f8 /packages/website/ts/utils/utils.ts | |
parent | 300c9f09b9ee129791a1f62881da0ee3c34ee0b5 (diff) | |
download | dexon-sol-tools-bc7e8ff471d7abe8e65b30d1945f6a989d521f10.tar dexon-sol-tools-bc7e8ff471d7abe8e65b30d1945f6a989d521f10.tar.gz dexon-sol-tools-bc7e8ff471d7abe8e65b30d1945f6a989d521f10.tar.bz2 dexon-sol-tools-bc7e8ff471d7abe8e65b30d1945f6a989d521f10.tar.lz dexon-sol-tools-bc7e8ff471d7abe8e65b30d1945f6a989d521f10.tar.xz dexon-sol-tools-bc7e8ff471d7abe8e65b30d1945f6a989d521f10.tar.zst dexon-sol-tools-bc7e8ff471d7abe8e65b30d1945f6a989d521f10.zip |
Add better balance formatting rules for balances and usd values
Diffstat (limited to 'packages/website/ts/utils/utils.ts')
-rw-r--r-- | packages/website/ts/utils/utils.ts | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/packages/website/ts/utils/utils.ts b/packages/website/ts/utils/utils.ts index 726e1815f..b27c6e48b 100644 --- a/packages/website/ts/utils/utils.ts +++ b/packages/website/ts/utils/utils.ts @@ -8,6 +8,8 @@ import * as bowser from 'bowser'; import deepEqual = require('deep-equal'); import * as _ from 'lodash'; import * as moment from 'moment'; +import * as numeral from 'numeral'; + import { AccountState, BlockchainCallErrs, @@ -380,10 +382,20 @@ export const utils = { }, getFormattedAmount(amount: BigNumber, decimals: number, symbol: string): string { const unitAmount = Web3Wrapper.toUnitAmount(amount, decimals); - const precision = Math.min(constants.TOKEN_AMOUNT_DISPLAY_PRECISION, unitAmount.decimalPlaces()); - const formattedAmount = unitAmount.toFixed(precision); + // if the unit amount is less than 1, show the natural number of decimal places with a max of 4 + // if the unit amount is greater than or equal to 1, show only 2 decimal places + const precision = unitAmount.lt(1) + ? Math.min(constants.TOKEN_AMOUNT_DISPLAY_PRECISION, unitAmount.decimalPlaces()) + : 2; + const format = `0,0.${_.repeat('0', precision)}`; + const formattedAmount = numeral(unitAmount).format(format); return `${formattedAmount} ${symbol}`; }, + getUsdValueFormattedAmount(amount: BigNumber, decimals: number, price: BigNumber): string { + const unitAmount = Web3Wrapper.toUnitAmount(amount, decimals); + const value = unitAmount.mul(price); + return numeral(value).format(constants.NUMERAL_USD_FORMAT); + }, openUrl(url: string): void { window.open(url, '_blank'); }, |