Skip to content

Commit 2a21556

Browse files
committed
feat(billing): add billions unit formatting for token display
Generated-By: PostHog Code
1 parent 0a7c297 commit 2a21556

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { describe, expect, it } from "vitest";
2+
import { formatTokens } from "./spendAnalysisFormat";
3+
4+
describe("formatTokens", () => {
5+
it.each([
6+
[0, "0"],
7+
[999, "999"],
8+
[1_000, "1k"],
9+
[108_400, "108k"],
10+
[1_500_000, "1.5M"],
11+
[999_949_999, "999.9M"],
12+
[1_000_000_000, "1.0B"],
13+
[2_449_300_000, "2.4B"],
14+
])("formats %d as %s", (input, expected) => {
15+
expect(formatTokens(input)).toBe(expected);
16+
});
17+
});

packages/core/src/billing/spendAnalysisFormat.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export function formatUsd(amount: number): string {
66
}
77

88
export function formatTokens(n: number): string {
9+
if (n >= 1_000_000_000) return `${(n / 1_000_000_000).toFixed(1)}B`;
910
if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(1)}M`;
1011
if (n >= 1_000) return `${(n / 1_000).toFixed(0)}k`;
1112
return n.toString();

0 commit comments

Comments
 (0)