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