Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ describe('MonitoringCenterPage summary cards', () => {
expect(html).toContain('_summaryGrid');
expect(html.match(/<strong/g)).toHaveLength(8);
expect(html).toContain('25.5K');
expect(html).toContain('1.9K');
expect(html).toContain('1900');
expect(html).toContain('2.8B');
expect(html).toContain('3.6B');
expect(html).toContain('role="tooltip"');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ const compactNumber = (value: number) => {
if (!Number.isFinite(value)) return '0';
if (Math.abs(value) >= 1_000_000_000) return `${(value / 1_000_000_000).toFixed(2)}B`;
if (Math.abs(value) >= 1_000_000) return `${(value / 1_000_000).toFixed(2)}M`;
if (Math.abs(value) >= 1_000) return `${(value / 1_000).toFixed(1)}K`;
if (Math.abs(value) >= 10_000) return `${(value / 1_000).toFixed(1)}K`;
return String(Math.round(value));
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ const formatCompactNumber = (value: number) => {
if (!Number.isFinite(value)) return '0';
if (Math.abs(value) >= 1_000_000_000) return `${(value / 1_000_000_000).toFixed(2)}B`;
if (Math.abs(value) >= 1_000_000) return `${(value / 1_000_000).toFixed(1)}M`;
if (Math.abs(value) >= 1_000) return `${(value / 1_000).toFixed(1)}K`;
if (Math.abs(value) >= 10_000) return `${(value / 1_000).toFixed(1)}K`;
return String(Math.round(value));
};

Expand Down
4 changes: 3 additions & 1 deletion apps/web/src/utils/usage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import { maskSensitiveText } from './format';
describe('formatCompactNumber', () => {
it('keeps large values compact as data grows beyond millions', () => {
expect(formatCompactNumber(999)).toBe('999');
expect(formatCompactNumber(1_200)).toBe('1.2K');
expect(formatCompactNumber(1_200)).toBe('1200');
expect(formatCompactNumber(9_999)).toBe('9999');
expect(formatCompactNumber(12_000)).toBe('12.0K');
expect(formatCompactNumber(999_950)).toBe('1.0M');
expect(formatCompactNumber(2_795_200_000)).toBe('2.8B');
expect(formatCompactNumber(1_200_000_000_000)).toBe('1.2T');
Expand Down
14 changes: 7 additions & 7 deletions apps/web/src/utils/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -844,19 +844,19 @@ export function formatCompactNumber(value: number): string {
const abs = Math.abs(num);
if (abs === 0) return '0';
const units = [
{ threshold: 1_000_000_000_000_000, suffix: 'P' },
{ threshold: 1_000_000_000_000, suffix: 'T' },
{ threshold: 1_000_000_000, suffix: 'B' },
{ threshold: 1_000_000, suffix: 'M' },
{ threshold: 1_000, suffix: 'K' },
{ threshold: 1_000_000_000_000_000, divisor: 1_000_000_000_000_000, suffix: 'P' },
{ threshold: 1_000_000_000_000, divisor: 1_000_000_000_000, suffix: 'T' },
{ threshold: 1_000_000_000, divisor: 1_000_000_000, suffix: 'B' },
{ threshold: 1_000_000, divisor: 1_000_000, suffix: 'M' },
{ threshold: 10_000, divisor: 1_000, suffix: 'K' },
];
const unit = units.find((item) => abs >= item.threshold);

if (unit) {
const formatted = (num / unit.threshold).toFixed(1);
const formatted = (num / unit.divisor).toFixed(1);
const nextUnit = units[units.indexOf(unit) - 1];
if (nextUnit && Math.abs(Number(formatted)) >= 1000) {
return `${(num / nextUnit.threshold).toFixed(1)}${nextUnit.suffix}`;
return `${(num / nextUnit.divisor).toFixed(1)}${nextUnit.suffix}`;
}
return `${formatted}${unit.suffix}`;
}
Expand Down