feat(client): step token figures up to billions in fmt
Values at 1e9 and above rendered as four-digit M figures (1055.9M); fmt now steps to a one-decimal B suffix, same style as k and M.
This commit is contained in:
1 parent
0003ceabaa
commit
77839c8d3f
2 files changed
+7
-3
No files matched your search
@@ -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))
|
||||
|
||||
@@ -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')
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
Reference in new issue
Block a user