Mike Rockétt

A package for V that converts bytes into human-friendly strings.

Install

$ v install mikerockett.humanbytes

Use

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

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