Human Bytes
github.com/mikerockett/human_bytesA package for V that converts bytes into human-friendly strings.
Install
$ v install mikerockett.humanbytes
Use
import human_bytes { convert, convert_opt } // Normal conversionsconvert(10) // 10 Bconvert(1001) // 1 kB // Conversions with optionsconvert_opt(1001, signed: true) // +1 kBconvert_opt(1001, bits: true) // 1 kbitconvert_opt(1e16, binary: true) // 8.88 PiBconvert_opt(1025, bits: true, binary: true, signed: true) // +1 kibit
API
Note that it uses base-10 (e.g. kilobyte). Read about the difference between kilobyte and kibibyte.
convert(input_val)
input_val f64
The number to format.
convert_opt(input_val, options)
options struct Options
signed bool = false
Include plus sign for positive numbers. If the difference is exactly zero a space character will be prepended instead for better alignment.
bits bool = false
Format the number as bits instead of bytes. This can be useful when, for example, referring to bit rate.
binary bool = false
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.
Thanks
This work is based on pretty-bytes for JavaScript by Sindre.
Published 18 July 2021 in V