src/global/functions/viewcount_text.rs

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

19 lines · 504 bytes

2 symbols in this file
 1const UNITS: &[(&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 `1560000`
 9pub fn viewcount_text(views: u64) -> String {
10    for unit in UNITS.iter() {
11        if unit.1 == 1_f64 {
12            return views.to_string();
13        } else if views >= unit.1 as u64 {
14            return format!("{:.2}{}", views as f64 / unit.1, unit.0);
15        }
16    }
17
18    String::from("No views")
19}