fix(web): stop formatUsageNumber rendering 1000.0K at unit boundaries (#1340)

Co-authored-by: Vedant Mahajan <vedant.04.mahajan@gmail.com>
This commit is contained in:
Abhay Singh 2026-08-12 20:57:33 +05:30 committed by GitHub
parent 47152afc1d
commit 00e57fb9c2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -185,18 +185,26 @@ export function getBrainTrialInfo(
}
/**
* Format a number with K/M suffix for display
* Format a number with K/M/B suffix for display
* @example formatUsageNumber(1500000) => "1.5M"
* @example formatUsageNumber(50000) => "50K"
* @example formatUsageNumber(999950) => "1.0M"
*/
export function formatUsageNumber(value: number): string {
const withSuffix = (n: number, suffix: string) =>
n % 1 === 0 ? `${n}${suffix}` : `${n.toFixed(1)}${suffix}`
if (value >= 1_000_000) {
const millions = value / 1_000_000
return millions % 1 === 0 ? `${millions}M` : `${millions.toFixed(1)}M`
return millions >= 999.95
? withSuffix(value / 1_000_000_000, "B")
: withSuffix(millions, "M")
}
if (value >= 1_000) {
const thousands = value / 1_000
return thousands % 1 === 0 ? `${thousands}K` : `${thousands.toFixed(1)}K`
return thousands >= 999.95
? withSuffix(value / 1_000_000, "M")
: withSuffix(thousands, "K")
}
return value.toString()
}