src/global/functions/viewcount_text.rs

db597a03bc7a1786d5261e8a55628da30193c775
youtube-tui/src/global/functions/viewcount_text.rs history · blame · raw

17 lines · 429 bytes

 1const UNITS: &[(&'static str, f64)] = &[
 2    ("B", 1_000_000_000_f64),
 3    ("M", 1_000_000_f64),
 4    ("K", 1_000_f64),
 5    ("", 1_f64),
 6];
 7
 8// Turns number into short hand like 1.56M instead of its full form
 9pub fn viewcount_text(views: u64) -> String {
10    for unit in UNITS.iter() {
11        if views >= unit.1 as u64 {
12            return format!("{:.2}{}", views as f64 / unit.1, unit.0);
13        }
14    }
15
16    unreachable!()
17}