Skip to content

Commit

Permalink
chore: more price formatting changes
Browse files Browse the repository at this point in the history
  • Loading branch information
borcherd committed Jan 7, 2025
1 parent 6f8f816 commit 580c4fa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ export const PoolCard = ({ poolData }: PoolCardProps) => {
<dd className="text-right text-primary tracking-wide">
{dynamicNumeralFormatter(tokenData.price / quoteTokenData.price, {
ignoreMinDisplay: true,
})}
})}{" "}
{quoteTokenData.symbol}
{tokenData.priceChange24h && (
<span
className={cn("text-xs ml-1", tokenData.priceChange24h > 0 ? "text-mrgn-success" : "text-mrgn-error")}
Expand Down
17 changes: 13 additions & 4 deletions packages/mrgn-common/src/utils/formatters.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,20 @@ export const dynamicNumeralFormatter = (value: number, options: dynamicNumeralFo
return numeral(value).format("0,0.[0000]a"); // Format with up to 4 decimal places
}

// New behavior: Ensure 3 significant digits when ignoreMinDisplay is true
// New behavior: Ensure 3 significant decimal digits when ignoreMinDisplay is true
if (ignoreMinDisplay) {
const exponent = Math.floor(Math.log10(absValue)); // using floor for more precision
const significantDecimals = Math.max(0, 2 - exponent);
return value.toFixed(significantDecimals).replace(/\.?0+$/, "");
const decimalPart = absValue - Math.floor(absValue);
const decimalPlaces = decimalPart > 0 ? 3 : 0;

if (absValue >= 1) {
// For values >= 1, format the number with 3 decimal digits
return value.toFixed(decimalPlaces).replace(/\.?0+$/, "");
} else {
// For values < 1, calculate the significant decimal digits precisely
const exponent = Math.floor(Math.log10(absValue));
const significantDecimals = Math.max(3, 2 - exponent); // Ensure 3 significant decimal digits
return value.toFixed(significantDecimals).replace(/\.?0+$/, "");
}
}

// Case: Token price takes priority when defined
Expand Down

0 comments on commit 580c4fa

Please sign in to comment.