prettybytes

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 4, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package prettybytes converts a number of bytes to a human readable string.

It is a faithful port of the npm package "pretty-bytes". By default it uses SI units (base 1000): B, kB, MB, GB, TB, PB, EB, ZB, YB and renders numbers with up to three significant digits (mirroring JavaScript's toPrecision(3)).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PrettyBytes

func PrettyBytes(n float64) string

PrettyBytes converts a number of bytes into a human readable string using SI units (base 1000). For example PrettyBytes(1337) returns "1.34 kB".

func PrettyBytesOpts

func PrettyBytesOpts(number float64, opts Options) string

PrettyBytesOpts converts a number of bytes into a human readable string honouring the supplied Options.

Types

type Options

type Options struct {
	// Signed shows a "+" in front of positive numbers (and " " for zero).
	Signed bool
	// Bits uses bit units (b, kbit, ...) instead of byte units.
	Bits bool
	// Binary uses base 1024 units (KiB, MiB, ... or kibit, ...).
	Binary bool
	// MinimumFractionDigits forces a minimum number of fraction digits.
	// When nil, no minimum is imposed.
	MinimumFractionDigits *int
	// MaximumFractionDigits caps the number of fraction digits. When nil the
	// default toPrecision(3) behaviour is used.
	MaximumFractionDigits *int
}

Options configures how a byte count is rendered.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL