diff --git a/src/client/format.ts b/src/client/format.ts index 2fd6078..08dbc24 100644 --- a/src/client/format.ts +++ b/src/client/format.ts @@ -1,9 +1,10 @@ -/** `fmt`: the k/M suffix style shared by bars/details/stats; `fmtTime`: local HH:MM:SS. */ +/** `fmt`: the k/M/B suffix style shared by bars/details/stats; `fmtTime`: local HH:MM:SS. */ export function fmt(n: number | null | undefined): string { if (n === undefined || n === null || isNaN(n)) return '—' const sign = n < 0 ? '-' : '' const a = Math.abs(n) + if (a >= 1e9) return sign + (a / 1e9).toFixed(1) + 'B' if (a >= 1e6) return sign + (a / 1e6).toFixed(1) + 'M' if (a >= 1000) return sign + (a / 1000).toFixed(1) + 'k' return sign + String(Math.round(a)) diff --git a/tests/client/format.spec.ts b/tests/client/format.spec.ts index e0b46fb..a1e0746 100644 --- a/tests/client/format.spec.ts +++ b/tests/client/format.spec.ts @@ -1,4 +1,4 @@ -// Display formatting (src/client/format.ts): the k/M suffix style, byte +// Display formatting (src/client/format.ts): the k/M/B suffix style, byte // sizes, the cache-hit truncation, locale-safe time, locale-unit durations, // and leading slice shares. @@ -18,16 +18,19 @@ describe('fmt', () => { assert.equal(fmt(999.4), '999') }) - test('thousands and millions take suffixes with one decimal', () => { + test('thousands, millions, and billions take suffixes with one decimal', () => { assert.equal(fmt(1000), '1.0k') assert.equal(fmt(1534), '1.5k') assert.equal(fmt(1_000_000), '1.0M') assert.equal(fmt(128_000), '128.0k') + assert.equal(fmt(1_000_000_000), '1.0B') + assert.equal(fmt(1_055_900_000), '1.1B') }) test('negatives keep their sign', () => { assert.equal(fmt(-1500), '-1.5k') assert.equal(fmt(-12), '-12') + assert.equal(fmt(-2_000_000_000), '-2.0B') }) })