format_bytes

Function format_bytes 

Source
pub fn format_bytes(bytes: u64) -> String
Expand description

Formats a byte count into a human-readable string with appropriate units.

This function converts raw byte counts into a more readable format using standard binary units (B, KB, MB, GB). It automatically selects the most appropriate unit and formats the output with appropriate decimal precision.

§Arguments

  • bytes - The number of bytes to format

§Returns

A formatted string representation of the byte count with units.

  • Bytes are shown as whole numbers (e.g., “512 B”)
  • Larger units are shown with one decimal place (e.g., “1.5 KB”)

§Examples

use image_optimizer::file_ops::format_bytes;

assert_eq!(format_bytes(512), "512 B");
assert_eq!(format_bytes(1024), "1.0 KB");
assert_eq!(format_bytes(1536), "1.5 KB");
assert_eq!(format_bytes(1048576), "1.0 MB");