Fix USD cost formatting for large values

This commit is contained in:
musistudio 2026-07-09 15:59:08 +08:00
parent 980a9bae63
commit ab6a2c1c50
3 changed files with 11 additions and 3 deletions

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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,