fix: keep only two decimal places at most for balance (#3564)

* fix: keep only two decimal places at most for balance

* refactor: use mono font styling for balance
This commit is contained in:
Whitewater
2025-04-23 17:43:50 +00:00
committed by GitHub
parent 634436ee83
commit 55de55e715
3 changed files with 17 additions and 12 deletions
@@ -37,7 +37,9 @@ export const Balance = ({
const Content = (
<span className={cn("tabular-nums", className)}>
{withSuffix && <i className="i-mgc-power text-accent mr-1 -translate-y-px align-middle" />}
<span>{withTooltip ? toScientificNotation(n, scientificThreshold, locale) : formatted}</span>
<span className="font-mono">
{withTooltip ? toScientificNotation(n, scientificThreshold, locale) : formatted}
</span>
</span>
)
@@ -47,7 +49,7 @@ export const Balance = ({
<Tooltip>
<TooltipTrigger asChild>{Content}</TooltipTrigger>
<TooltipContent>
<div className="text-sm">
<div className="font-mono text-sm">
<span className="font-bold tabular-nums">{formattedFull}</span> <span>Power</span>
</div>
</TooltipContent>
+11 -9
View File
@@ -29,9 +29,9 @@ describe("utils", () => {
test("toScientificNotation", () => {
// Test numbers below threshold (should return original number)
expect(toScientificNotation(123n, 3)).toBe("123")
expect(toScientificNotation(999n, 3)).toBe("999")
expect(toScientificNotation(1234n, 5)).toBe("1,234")
expect(toScientificNotation(123n, 3)).toBe("123.00")
expect(toScientificNotation(999n, 3)).toBe("999.00")
expect(toScientificNotation(1234n, 5)).toBe("1,234.00")
// Test numbers above threshold (should convert to scientific notation)
expect(toScientificNotation(1234n, 3)).toBe("1.23e+3")
@@ -40,14 +40,13 @@ describe("utils", () => {
// Test with decimal numbers
expect(toScientificNotation([123456n, 2], 3)).toBe("1.23e+3")
expect(toScientificNotation([1234n, 4], 3)).toBe("0.1234")
// Test with leading zeros
expect(toScientificNotation(1234n, 3)).toBe("1.23e+3")
expect(toScientificNotation([1234n, 4], 3)).toBe("0.1234")
expect(toScientificNotation([1234n, 4], 3)).toBe("0.12")
// Test with larger threshold
expect(toScientificNotation(12345678n, 8)).toBe("12,345,678")
expect(toScientificNotation(12345678n, 8)).toBe("12,345,678.00")
expect(toScientificNotation(123456789n, 8)).toBe("1.23e+8")
// Edge cases
@@ -71,10 +70,13 @@ describe("utils", () => {
expect(toScientificNotation(1234567n, 3, "en-US")).toBe("1.23e+6")
// Test with integer numbers below threshold with different locales
expect(toScientificNotation(1234n, 5, "en-US")).toBe("1,234")
expect(toScientificNotation(1234n, 5, "de-DE")).toBe("1.234")
expect(toScientificNotation(1234n, 5, "en-US")).toBe("1,234.00")
expect(toScientificNotation(1234n, 5, "de-DE")).toBe("1.234,00")
expect(toScientificNotation(1234n, 5, "fr-FR")).toBe("1 234")
expect(toScientificNotation(1234n, 5, "fr-FR")).toBe("1 234,00")
// Test with real cases
expect(toScientificNotation([60386874408275410679920n, 18], 6)).toBe("60,386.87")
})
test("omitShallow", () => {
+2 -1
View File
@@ -291,7 +291,8 @@ export const toScientificNotation = (
// Use provided locale or default to en-US
const localeString = locale?.toString() || "en-US"
const formatter = new Intl.NumberFormat(localeString, {
maximumFractionDigits: decimals,
maximumFractionDigits: 2,
minimumFractionDigits: 2,
})
// Convert bigint to number with correct decimal places