From ab6a2c1c501627ff23e1052a46c636b3aa8fd285 Mon Sep 17 00:00:00 2001 From: musistudio Date: Thu, 9 Jul 2026 15:59:08 +0800 Subject: [PATCH] Fix USD cost formatting for large values --- packages/ui/src/pages/home/shared/usage.ts | 2 +- packages/ui/src/pages/tray/shared.tsx | 2 +- tests/renderer/usage-format.test.ts | 10 +++++++++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/ui/src/pages/home/shared/usage.ts b/packages/ui/src/pages/home/shared/usage.ts index 040988d..ed29c9b 100644 --- a/packages/ui/src/pages/home/shared/usage.ts +++ b/packages/ui/src/pages/home/shared/usage.ts @@ -155,7 +155,7 @@ export function formatUsdCost(value: number | undefined): string { return new Intl.NumberFormat(undefined, { currency: "USD", maximumFractionDigits: normalized >= 100 ? 0 : 2, - minimumFractionDigits: 2, + minimumFractionDigits: normalized >= 100 ? 0 : 2, style: "currency" }).format(normalized); } diff --git a/packages/ui/src/pages/tray/shared.tsx b/packages/ui/src/pages/tray/shared.tsx index 1976d25..ef2cac4 100644 --- a/packages/ui/src/pages/tray/shared.tsx +++ b/packages/ui/src/pages/tray/shared.tsx @@ -609,7 +609,7 @@ export function formatUsdCost(value: number | undefined): string { return new Intl.NumberFormat(undefined, { currency: "USD", maximumFractionDigits: normalized >= 100 ? 0 : 2, - minimumFractionDigits: 2, + minimumFractionDigits: normalized >= 100 ? 0 : 2, style: "currency" }).format(normalized); } diff --git a/tests/renderer/usage-format.test.ts b/tests/renderer/usage-format.test.ts index 43eca8f..a6df6da 100644 --- a/tests/renderer/usage-format.test.ts +++ b/tests/renderer/usage-format.test.ts @@ -1,7 +1,8 @@ import assert from "node:assert/strict"; import test from "node:test"; import { formatLogTokenSummary } from "../../packages/ui/src/pages/home/shared/logs.ts"; -import { formatCompactNumber } from "../../packages/ui/src/pages/home/shared/usage.ts"; +import { formatCompactNumber, formatUsdCost as formatHomeUsdCost } from "../../packages/ui/src/pages/home/shared/usage.ts"; +import { formatUsdCost as formatTrayUsdCost } from "../../packages/ui/src/pages/tray/shared.tsx"; import type { RequestLogEntry } from "../../packages/core/src/contracts/app.ts"; test("formatCompactNumber can be bound to the UI language locale", () => { @@ -9,6 +10,13 @@ test("formatCompactNumber can be bound to the UI language locale", () => { assert.equal(formatCompactNumber(123456, "zh-CN"), "12.3δΈ‡"); }); +test("formatUsdCost formats large values without conflicting fraction digits", () => { + assert.doesNotThrow(() => formatHomeUsdCost(100)); + assert.doesNotThrow(() => formatTrayUsdCost(100)); + assert.doesNotMatch(formatHomeUsdCost(123.45), /[.,]45/); + assert.doesNotMatch(formatTrayUsdCost(123.45), /[.,]45/); +}); + test("formatLogTokenSummary uses the provided locale for token counts", () => { const entry: RequestLogEntry = { cacheReadTokens: 0,