exa/format.rs

25 lines
634 B
Rust
Raw Normal View History

static METRIC_PREFIXES: &'static [&'static str] = &[
"", "K", "M", "G", "T", "P", "E", "Z", "Y"
];
static IEC_PREFIXES: &'static [&'static str] = &[
"", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"
];
2014-05-26 10:08:33 +00:00
fn formatBytes(mut amount: u64, kilo: u64, prefixes: &[&str]) -> String {
let mut prefix = 0;
while amount > kilo {
amount /= kilo;
prefix += 1;
}
format!("{}{}", amount, prefixes[prefix])
}
2014-05-26 10:08:33 +00:00
pub fn formatBinaryBytes(amount: u64) -> String {
formatBytes(amount, 1024, IEC_PREFIXES)
}
2014-05-26 10:08:33 +00:00
pub fn formatDecimalBytes(amount: u64) -> String {
formatBytes(amount, 1000, METRIC_PREFIXES)
}