Converts bytes into human-friendly strings.
$ v install mikerockett.humanbytes
import human_bytes { convert, convert_opt }
// Normal conversions
convert(10) // 10 B
convert(1001) // 1 kB
// Conversions with options
convert_opt(1001, signed: true) // +1 kB
convert_opt(1001, bits: true) // 1 kbit
convert_opt(1e16, binary: true) // 8.88 PiB
convert_opt(1025, bits: true, binary: true, signed: true) // +1 kibit
Note that it uses base-10 (e.g. kilobyte). Read about the difference between kilobyte and kibibyte.
The number to format.
Include plus sign for positive numbers. If the difference is exactly zero a space character will be prepended instead for better alignment.
Format the number as bits instead of bytes. This can be useful when, for example, referring to bit rate.
Format the number using the Binary Prefix instead of the SI Prefix. This can be useful for presenting memory amounts. However, this should not be used for presenting file sizes.
This work is based on pretty-bytes for JavaScript by Sindre.