| 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 |
|
| 25 |
use core::fmt::Display; |
| 26 |
use unit_prefix::NumberPrefix; |
| 27 |
|
| 28 |
|
| 29 |
fn main() { |
| 30 |
// part one, decimal prefixes |
| 31 |
let mut n = 1_f64; |
| 32 |
for _ in 0..8 { |
| 33 |
n *= 1000_f64; |
| 34 |
|
| 35 |
let decimal = format_prefix(NumberPrefix::decimal(n)); |
| 36 |
let binary = format_prefix(NumberPrefix::binary(n)); |
| 37 |
println!("{:26} bytes is {} and {:10}", n, decimal, binary); |
| 38 |
} |
| 39 |
|
| 40 |
println!(); |
| 41 |
|
| 42 |
// part two, binary prefixes |
| 43 |
let mut n = 1_f64; |
| 44 |
for _ in 0..8 { |
| 45 |
n *= 1024_f64; |
| 46 |
|
| 47 |
let decimal = format_prefix(NumberPrefix::decimal(n)); |
| 48 |
let binary = format_prefix(NumberPrefix::binary(n)); |
| 49 |
println!("{:26} bytes is {} and {:10}", n, binary, decimal); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
fn format_prefix<T: Display>(np: NumberPrefix<T>) -> String { |
| 55 |
match np { |
| 56 |
NumberPrefix::Prefixed(prefix, n) => format!("{:.3} {}B", n, prefix), |
| 57 |
NumberPrefix::Standalone(bytes) => format!("{} bytes", bytes), |
| 58 |
} |
| 59 |
} |