From 55de55e715361709fcbd0c2cfd51c01694c35f99 Mon Sep 17 00:00:00 2001 From: Whitewater Date: Thu, 24 Apr 2025 01:43:50 +0800 Subject: [PATCH] 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 --- .../renderer/src/modules/wallet/balance.tsx | 6 ++++-- packages/utils/src/utils.spec.ts | 20 ++++++++++--------- packages/utils/src/utils.ts | 3 ++- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/apps/desktop/src/renderer/src/modules/wallet/balance.tsx b/apps/desktop/src/renderer/src/modules/wallet/balance.tsx index 9ea4ef011..9ec93b2ae 100644 --- a/apps/desktop/src/renderer/src/modules/wallet/balance.tsx +++ b/apps/desktop/src/renderer/src/modules/wallet/balance.tsx @@ -37,7 +37,9 @@ export const Balance = ({ const Content = ( {withSuffix && } - {withTooltip ? toScientificNotation(n, scientificThreshold, locale) : formatted} + + {withTooltip ? toScientificNotation(n, scientificThreshold, locale) : formatted} + ) @@ -47,7 +49,7 @@ export const Balance = ({ {Content} -
+
{formattedFull} Power
diff --git a/packages/utils/src/utils.spec.ts b/packages/utils/src/utils.spec.ts index 0b5cd0039..5baa9f7ac 100644 --- a/packages/utils/src/utils.spec.ts +++ b/packages/utils/src/utils.spec.ts @@ -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", () => { diff --git a/packages/utils/src/utils.ts b/packages/utils/src/utils.ts index 7d43c5e90..205d47ffe 100644 --- a/packages/utils/src/utils.ts +++ b/packages/utils/src/utils.ts @@ -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