diff --git a/packages/tui/src/util/locale.ts b/packages/tui/src/util/locale.ts index 569484e37308..f9569b2751a3 100644 --- a/packages/tui/src/util/locale.ts +++ b/packages/tui/src/util/locale.ts @@ -17,7 +17,8 @@ export function datetime(input: number): string { } export function number(num: number): string { - if (num >= 1000000) { + // 999,950 and up round to 1000.0K at one decimal place, so roll over to M there + if (num >= 999950) { return (num / 1000000).toFixed(1) + "M" } else if (num >= 1000) { return (num / 1000).toFixed(1) + "K" diff --git a/packages/tui/test/util/locale.test.ts b/packages/tui/test/util/locale.test.ts index e3d3396884b4..ca4c2c51b631 100644 --- a/packages/tui/test/util/locale.test.ts +++ b/packages/tui/test/util/locale.test.ts @@ -12,3 +12,12 @@ test("takes whole graphemes within terminal width", () => { expect(Locale.takeWidth("ab็•Œcd", 4)).toBe("ab็•Œ") expect(Locale.graphemes("a๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆb")).toEqual(["a", "๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ", "b"]) }) + +test("formats compact numbers and rolls over to M at the rounding boundary", () => { + expect(Locale.number(999)).toBe("999") + expect(Locale.number(1500)).toBe("1.5K") + expect(Locale.number(999949)).toBe("999.9K") + expect(Locale.number(999950)).toBe("1.0M") + expect(Locale.number(999999)).toBe("1.0M") + expect(Locale.number(2500000)).toBe("2.5M") +})