#[cfg(test)]
mod tests;
#[cfg(not(feature = "bibytes"))]
const SUFFIX: [&'static str; 9] = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
#[cfg(feature = "bibytes")]
const SUFFIX: [&'static str; 9] = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"];
pub fn human_bytes<T: Into<f64>>(size: T) -> String {
let size = size.into();
if size <= 0.0 {
return "0 B".to_string();
}
let base = size.log10() / 1024_f64.log10();
#[cfg(feature = "fast")]
let mut result = lexical::to_string((1024_f64.powf(base - base.floor()) * 10.0).round() / 10.0)
.trim_end_matches(".0")
.to_owned();
#[cfg(not(feature = "fast"))]
let mut result = format!("{:.1}", 1024_f64.powf(base - base.floor()),)
.trim_end_matches(".0")
.to_owned();
result.push(' ');
result.push_str(SUFFIX[base.floor() as usize]);
result
}