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
3 changes: 2 additions & 1 deletion packages/tui/src/util/locale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 9 additions & 0 deletions packages/tui/test/util/locale.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
Loading