No description
  • V 98.4%
  • AMPL 1.6%
Find a file
2021-03-21 16:19:38 +02:00
.github add workflow, remove vpkg 2021-03-21 16:15:39 +02:00
.editorconfig add editorconfig, format 2021-03-21 16:02:03 +02:00
human_bytes.v cleanup 2021-03-21 16:17:19 +02:00
human_bytes_test.v init 2021-01-10 13:06:05 +02:00
license.md init 2021-01-10 13:06:05 +02:00
readme.md remove vpkg – stale pr 2021-03-21 15:54:50 +02:00
v.mod v mod 1.0.2 (missed 1.0.1) 2021-03-21 16:19:38 +02:00

human_bytes

Converts bytes into human-friendly strings.

Note that it uses base-10 (e.g. kilobyte). Read about the difference between kilobyte and kibibyte.

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

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.