Skip to main content

format_compact

Function format_compact 

Source
pub fn format_compact(n: i64) -> String
Expand description

Formats an integer into a compact human string with a K/M/B/T suffix.

Targets a dense dashboard where column width is scarce: values under 1000 render verbatim, otherwise the largest fitting unit is used with 3 significant figures (2 / 1 / 0 decimals as the scaled value crosses 10 and 100), so the result stays within ~5 characters. Rounding that would reach 1000 within a unit is promoted to the next unit, so 999_950 renders as "1.00M" rather than "1000K". Negative values keep a leading -.

Only the interactive TUI uses this; the static table, text, and JSON paths keep format_number so their numbers stay exact.

ยงExamples

use vct_core::utils::format_compact;

assert_eq!(format_compact(999), "999");
assert_eq!(format_compact(1_000), "1.00K");
assert_eq!(format_compact(1_234_567), "1.23M");
assert_eq!(format_compact(999_950), "1.00M");