Format bytes in a human-readable way. # Arguments `bytes` - The number of bytes # Returns A formatted string like "1.5 KB" or "2.3 MB". # Example ```rust,ignore assert_eq!(format_bytes(512), "512 B"); assert_eq!(format_bytes(1536), "1.5 KB"); assert_eq!(format_bytes(2_500_000), "2.4 MB"); ```
(bytes: u64)
| 436 | /// assert_eq!(format_bytes(2_500_000), "2.4 MB"); |
| 437 | /// ``` |
| 438 | pub fn format_bytes(bytes: u64) -> String { |
| 439 | const KB: u64 = 1024; |
| 440 | const MB: u64 = KB * 1024; |
| 441 | const GB: u64 = MB * 1024; |
| 442 | |
| 443 | if bytes >= GB { |
| 444 | format!("{:.1} GB", bytes as f64 / GB as f64) |
| 445 | } else if bytes >= MB { |
| 446 | format!("{:.1} MB", bytes as f64 / MB as f64) |
| 447 | } else if bytes >= KB { |
| 448 | format!("{:.1} KB", bytes as f64 / KB as f64) |
| 449 | } else { |
| 450 | format!("{} B", bytes) |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | // Tests |
| 455 |
nothing calls this directly
no outgoing calls
no test coverage detected