Skip to content

Commit

Permalink
feat: format number (#5)
Browse files Browse the repository at this point in the history
closes ZKS-111
  • Loading branch information
0xtiti authored Jul 22, 2024
1 parent 9932945 commit ae66122
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/components/Table.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useTranslation } from 'next-i18next';

import { EcosystemChainData } from '~/types';
import { formatDataNumber } from '~/utils';

interface TableProps {
chains: EcosystemChainData[];
Expand All @@ -25,7 +26,7 @@ export const Table = ({ chains }: TableProps) => {
<td>{data.name}</td>
<td>{data.id}</td>
<td>{data.nativeToken}</td>
<td>{data.tvl}</td>
<td>{formatDataNumber(data.tvl, 0, true)}</td>
<td>{data.type}</td>
</tr>
);
Expand Down
4 changes: 3 additions & 1 deletion src/components/TotalValueLocked.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { formatDataNumber } from '~/utils';

export interface TokenValueLocked {
token: string;
value: number;
Expand All @@ -13,7 +15,7 @@ export const TotalValueLocked = ({ tvl }: TotalValueLockedProps) => {
{tvl.map((data, index) => (
<div key={index}>
<span>{data.token}</span>
<span>{data.value}</span>
<span>{formatDataNumber(data.value, 0, true)}</span>
</div>
))}
</div>
Expand Down
3 changes: 2 additions & 1 deletion src/containers/LockedAssets/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useTranslation } from 'next-i18next';

import { TotalValueLocked, Title } from '~/components';
import { useData } from '~/hooks';
import { formatDataNumber } from '~/utils';

export const LockedAssets = () => {
const { t } = useTranslation();
Expand All @@ -11,7 +12,7 @@ export const LockedAssets = () => {
<section>
{ecosystemData && (
<>
<Title title={`${t('HOME.lockedAssets')}: ${ecosystemData.total}`} />
<Title title={`${t('HOME.lockedAssets')}: ${formatDataNumber(ecosystemData.total, 0, true, true)}`} />
<TotalValueLocked tvl={ecosystemData.tvl} />
</>
)}
Expand Down
2 changes: 1 addition & 1 deletion src/data/ecosystemMockData.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"type": "Validium"
}
],
"total": 7000000,
"total": 700000000,
"tvl": [
{
"token": "ETH",
Expand Down
43 changes: 43 additions & 0 deletions src/utils/format.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,46 @@
export const truncateAddress = (address: string) => {
return `${address.slice(0, 6)}...${address.slice(-4)}`;
};

export function formatDataNumber(input: string | number, formatDecimal = 3, currency?: boolean, compact?: boolean) {
const res: number = Number.parseFloat(input.toString());

if (res === 0 || isNaN(res)) return `${currency ? '$0' : '0'}`;

if (res < 0.01) return formatSmallNumber(res);

const userNotation = compact ? 'compact' : 'standard';
const notation = res > 1e12 ? 'scientific' : userNotation;

return new Intl.NumberFormat('en-US', {
maximumFractionDigits: formatDecimal,
notation: notation,
style: currency ? 'currency' : 'decimal',
currency: 'USD',
}).format(res);
}

export const formatSmallNumber = (value: number) => {
if (value === 0) {
return '0';
}

const formattedValue = value.toString();

let numLeadingZeros = 0;

// Count the leading zeros and decimal point
for (let i = 0; i < formattedValue.length; i++) {
if (formattedValue[i] === '0' || formattedValue[i] === '.') {
numLeadingZeros++;
} else {
break;
}
}

// Return the number with 3 digits after the last leading zero
const result = formattedValue.slice(0, numLeadingZeros + 3);

// Trim any trailing zeros from the result
return result.replace(/\.?0+$/, '');
};

0 comments on commit ae66122

Please sign in to comment.