conversions/conversions.rs
1/// This example prints out the conversions for increasingly-large numbers, to
2/// showcase how the numbers change as the input gets bigger.
3/// It results in this:
4///
5/// ```text
6/// 1000 bytes is 1.000 kB and 1000 bytes
7/// 1000000 bytes is 1.000 MB and 976.562 KiB
8/// 1000000000 bytes is 1.000 GB and 953.674 MiB
9/// 1000000000000 bytes is 1.000 TB and 931.323 GiB
10/// 1000000000000000 bytes is 1.000 PB and 909.495 TiB
11/// 1000000000000000000 bytes is 1.000 EB and 888.178 PiB
12/// 1000000000000000000000 bytes is 1.000 ZB and 867.362 EiB
13/// 1000000000000000000000000 bytes is 1.000 YB and 847.033 ZiB
14///
15/// 1024 bytes is 1.000 KiB and 1.024 kB
16/// 1048576 bytes is 1.000 MiB and 1.049 MB
17/// 1073741824 bytes is 1.000 GiB and 1.074 GB
18/// 1099511627776 bytes is 1.000 TiB and 1.100 TB
19/// 1125899906842624 bytes is 1.000 PiB and 1.126 PB
20/// 1152921504606847000 bytes is 1.000 EiB and 1.153 EB
21/// 1180591620717411300000 bytes is 1.000 ZiB and 1.181 ZB
22/// 1208925819614629200000000 bytes is 1.000 YiB and 1.209 YB
23/// ```
24
25extern crate number_prefix;
26use number_prefix::NumberPrefix;
27use std::fmt::Display;
28
29
30fn main() {
31
32 // part one, decimal prefixes
33 let mut n = 1_f64;
34 for _ in 0 .. 8 {
35 n *= 1000_f64;
36
37 let decimal = format_prefix(NumberPrefix::decimal(n));
38 let binary = format_prefix(NumberPrefix::binary(n));
39 println!("{:26} bytes is {} and {:10}", n, decimal, binary);
40 }
41
42 println!();
43
44 // part two, binary prefixes
45 let mut n = 1_f64;
46 for _ in 0 .. 8 {
47 n *= 1024_f64;
48
49 let decimal = format_prefix(NumberPrefix::decimal(n));
50 let binary = format_prefix(NumberPrefix::binary(n));
51 println!("{:26} bytes is {} and {:10}", n, binary, decimal);
52 }
53}
54
55
56fn format_prefix<T: Display>(np: NumberPrefix<T>) -> String {
57 match np {
58 NumberPrefix::Prefixed(prefix, n) => format!("{:.3} {}B", n, prefix),
59 NumberPrefix::Standalone(bytes) => format!("{} bytes", bytes),
60 }
61}