summaryrefslogtreecommitdiffstats
path: root/src/display/num_format.rs
blob: d8f5bc53dea4048ea15d121fb9d9b84d68b4304a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/// Format a number with commas as thousands separators
pub fn format_count(count: usize) -> String {
    let mut s = count.to_string();
    let l = s.len();
    for i in 1..l {
        if i % 3 == 0 {
            s.insert(l-i, ',');
        }
    }
    s
}

#[test]
fn test_format_count() {
    assert_eq!(&format_count(1), "1");
    assert_eq!(&format_count(12), "12");
    assert_eq!(&format_count(123), "123");
    assert_eq!(&format_count(1234), "1,234");
    assert_eq!(&format_count(12345), "12,345");
    assert_eq!(&format_count(123456), "123,456");
    assert_eq!(&format_count(1234567), "1,234,567");
    assert_eq!(&format_count(12345678), "12,345,678");
    assert_eq!(&format_count(1234567890), "1,234,567,890");
}