fn format_count_internal()

in rd-util/src/lib.rs [382:416]


fn format_count_internal<T>(count: T, zero: &str) -> String
where
    T: num::ToPrimitive,
{
    let format_count_helper = |count: u64, zeroes: u32, suffix: &str| -> Option<String> {
        let unit: u64 = 10_u64.pow(zeroes);

        if (count as f64 / unit as f64) < 99.95 {
            Some(format!(
                "{:.1}{}",
                (count as f64 / unit as f64).max(0.1),
                suffix
            ))
        } else if (count as f64 / unit as f64) < 1000.0 {
            Some(format!("{:.0}{}", count as f64 / unit as f64, suffix))
        } else {
            None
        }
    };

    let count = count.to_u64().unwrap();

    if count == 0 {
        zero.to_string()
    } else if count < 1000 {
        format!("{}", count)
    } else {
        format_count_helper(count, 3, "k")
            .or_else(|| format_count_helper(count, 6, "m"))
            .or_else(|| format_count_helper(count, 9, "g"))
            .or_else(|| format_count_helper(count, 12, "p"))
            .or_else(|| format_count_helper(count, 15, "e"))
            .unwrap_or_else(|| "INF".into())
    }
}