summaryrefslogtreecommitdiffstats
path: root/vendor/number_prefix/examples
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 18:31:44 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 18:31:44 +0000
commitc23a457e72abe608715ac76f076f47dc42af07a5 (patch)
tree2772049aaf84b5c9d0ed12ec8d86812f7a7904b6 /vendor/number_prefix/examples
parentReleasing progress-linux version 1.73.0+dfsg1-1~progress7.99u1. (diff)
downloadrustc-c23a457e72abe608715ac76f076f47dc42af07a5.tar.xz
rustc-c23a457e72abe608715ac76f076f47dc42af07a5.zip
Merging upstream version 1.74.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/number_prefix/examples')
-rw-r--r--vendor/number_prefix/examples/conversions.rs61
1 files changed, 61 insertions, 0 deletions
diff --git a/vendor/number_prefix/examples/conversions.rs b/vendor/number_prefix/examples/conversions.rs
new file mode 100644
index 000000000..422d116a5
--- /dev/null
+++ b/vendor/number_prefix/examples/conversions.rs
@@ -0,0 +1,61 @@
+/// 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
+/// ```
+
+extern crate number_prefix;
+use number_prefix::NumberPrefix;
+use std::fmt::Display;
+
+
+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),
+ }
+}