From 9c2954cfc0c48da15734bd3ea9b6c850c057fdc2 Mon Sep 17 00:00:00 2001 From: Christopher Cali Date: Fri, 7 Jul 2023 08:35:26 -0400 Subject: [PATCH] Format numbers in overview chart --- apps/web/src/composables/contracts.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/apps/web/src/composables/contracts.ts b/apps/web/src/composables/contracts.ts index 656b3d651..8528d36fa 100644 --- a/apps/web/src/composables/contracts.ts +++ b/apps/web/src/composables/contracts.ts @@ -84,9 +84,11 @@ export default function useContracts() { const totalStaked = currentStaked.reduce((accumulator, currentValue) => accumulator.add(currentValue), ethers.BigNumber.from(0)) const totalStakedUSD = parseFloat(ethers.utils.formatEther(totalStaked)) * (await getCurrentPrice({ coin: 'ETH', currency: 'USD' })) const totalStakedETH = parseFloat(ethers.utils.formatEther(totalStaked)) + const formattedTotalStakedUSD = formatNumber(totalStakedUSD) + const formattedTotalStakedETH = formatNumber(totalStakedETH) return { - exchange: totalStakedETH.toFixed(2) + ' ETH', - usd: '$ ' + totalStakedUSD.toFixed(2) + exchange: formattedTotalStakedUSD + ' ETH', + usd: '$ ' + formattedTotalStakedETH } } catch (error) { console.log('Error occurred while fetching stake:', error) @@ -193,9 +195,11 @@ export default function useContracts() { addresses.forEach((address) => { promises.push(getEthersBalance(address)) }) const totalWalletBalance = (await Promise.all(promises)).reduce((acc, curr) => acc + curr, 0) const totalWalletBalanceUSD = totalWalletBalance * (await getCurrentPrice({ coin: 'ETH', currency: 'USD' })) + const formattedTotalWalletBalance = formatNumber(totalWalletBalance) + const formattedTotalWalletBalanceUSD = formatNumber(totalWalletBalanceUSD) return { - exchange: totalWalletBalance.toFixed(2) + ' ETH', - usd: '$ ' + totalWalletBalanceUSD.toFixed(2) + exchange: formattedTotalWalletBalance + ' ETH', + usd: '$ ' + formattedTotalWalletBalanceUSD } } @@ -296,4 +300,8 @@ export default function useContracts() { refreshBreakdown, withdraw } +} + +function formatNumber(number: number) { + return number.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,') } \ No newline at end of file