Skip to main content

human_size

Function human_size 

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

Formats a byte count for people, in powers of 1024 like Rails’ number_to_human_size.

Below 1024 the count is exact (1 byte, 512 bytes); above, it is rounded to one decimal, dropped when it is zero (2 KB, 1.5 MB, 10 GB). Units stop at TB. Used in the “is too large (maximum is 5 MB)” validation message and the 413 of Multipart.

§Examples

use ocre::storage::human_size;

assert_eq!(human_size(1), "1 byte");
assert_eq!(human_size(512), "512 bytes");
assert_eq!(human_size(2048), "2 KB");
assert_eq!(human_size(1536 * 1024), "1.5 MB");
assert_eq!(human_size(10 * 1024 * 1024), "10 MB");
assert_eq!(human_size(3 * 1024_u64.pow(5)), "3072 TB");