From 5272d4bfddc8fd1b01008ab2a9af2d08ae3eae55 Mon Sep 17 00:00:00 2001 From: iamdavidhill <1879069+iamdavidhill@users.noreply.github.com> Date: Mon, 28 Sep 2026 16:01:06 +0000 Subject: [PATCH] fix(stats): show one decimal for trillion-token usage --- packages/stats/app/src/routes/index.tsx | 7 ++++--- packages/stats/app/src/routes/token-format.test.ts | 9 +++++++++ packages/stats/app/src/routes/token-format.ts | 3 +++ 3 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 packages/stats/app/src/routes/token-format.test.ts create mode 100644 packages/stats/app/src/routes/token-format.ts diff --git a/packages/stats/app/src/routes/index.tsx b/packages/stats/app/src/routes/index.tsx index ffa7412debb..7f477f5310c 100644 --- a/packages/stats/app/src/routes/index.tsx +++ b/packages/stats/app/src/routes/index.tsx @@ -24,6 +24,7 @@ import { findModelCatalogEntry, loadModelCatalog, type ModelCatalog } from "./mo import { geoMapHeight, geoMapWidth, worldBorderPath, worldCountryMarkers, worldCountryPaths } from "./geo-map" import { SectionHeading } from "./section-heading" import { setStatsPageCacheHeaders } from "./stats-cache" +import { formatTrillionTokens } from "./token-format" import { ComparisonCardsSection, uniqueComparisonPairs, type ComparisonModelRef } from "./compare-cards" import { applyThemePreference, @@ -703,7 +704,7 @@ function usageTotal(point: UsagePoint) { } function formatTokens(value: number) { - if (value >= 1) return `${value.toFixed(value >= 10 ? 0 : 1)}T` + if (value >= 1) return formatTrillionTokens(value) return `${Math.round(value * 1000)}B` } @@ -832,7 +833,7 @@ function getProviderIconId(author: string) { } function formatBillions(value: number) { - if (value >= 1000) return `${(value / 1000).toFixed(value >= 10000 ? 0 : 1)}T` + if (value >= 1000) return formatTrillionTokens(value / 1000) return `${value}B` } @@ -1334,7 +1335,7 @@ function formatTrillions(value: number) { if (value === 0) return "0" if (value < 0.001) return `${Number((value * 1_000_000).toFixed(value >= 0.00001 ? 0 : 1))}M` if (value < 1) return `${Number((value * 1_000).toFixed(value >= 0.01 ? 0 : 1))}B` - return `${value.toFixed(value >= 10 ? 0 : 1)}T` + return formatTrillionTokens(value) } function formatMarketDate(day: MarketDay | undefined, fallback: string) { diff --git a/packages/stats/app/src/routes/token-format.test.ts b/packages/stats/app/src/routes/token-format.test.ts new file mode 100644 index 00000000000..63c7bf85688 --- /dev/null +++ b/packages/stats/app/src/routes/token-format.test.ts @@ -0,0 +1,9 @@ +import { expect, test } from "bun:test" +import { formatTrillionTokens } from "./token-format" + +test("formats trillion-token usage with one decimal, including whole totals", () => { + expect(formatTrillionTokens(1)).toBe("1.0T") + expect(formatTrillionTokens(9.15)).toBe("9.2T") + expect(formatTrillionTokens(12)).toBe("12.0T") + expect(formatTrillionTokens(21.34)).toBe("21.3T") +}) diff --git a/packages/stats/app/src/routes/token-format.ts b/packages/stats/app/src/routes/token-format.ts new file mode 100644 index 00000000000..7377d049ea6 --- /dev/null +++ b/packages/stats/app/src/routes/token-format.ts @@ -0,0 +1,3 @@ +export function formatTrillionTokens(value: number) { + return `${value.toFixed(1)}T` +}