bytefmt

package
v0.0.0-...-00a1424 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2025 License: MIT Imports: 4 Imported by: 6

Documentation

Index

Constants

View Source
const (
	KiB
	MiB
	GiB
	TiB
	PiB
	EiB
)

Binary units (IEC 60027).

View Source
const (
	KB = 1000
	MB = KB * 1000
	GB = MB * 1000
	TB = GB * 1000
	PB = TB * 1000
	EB = PB * 1000
)

Decimal units (SI international system of units).

View Source
const (
	DefaultPrecision      = 2
	DefaultTrimIntDecimal = true
	DefaultNoSpace        = false
)

Variables

View Source
var ErrUnknownSuffix = errors.New("unknown suffix")

Functions

func Decode

func Decode(text string) (int64, error)

Decode parses human-readable bytes string to bytes integer.

For example, "6 GiB" ("6 Gi" is also valid) will return 6442450944, and "6 GB" ("6 G" is also valid) will return 6000000000. The input case-insensitive, and the space is optional, so "6GiB" or "6GB" would produce the same output.

func DecodeBinary

func DecodeBinary(text string) (int64, error)

DecodeBinary parses human-readable bytes string to bytes integer.

For example, "6 GiB" ("6 Gi" is also valid) will return 6442450944. The input case-insensitive, and the space is optional, so "6GiB" would produce the same output.

func DecodeDecimal

func DecodeDecimal(text string) (int64, error)

DecodeDecimal parses human-readable bytes string to bytes integer.

For example, "6 GB" ("6 G" is also valid) will return 6000000000. The input case-insensitive, and the space is optional, so "6GB" would produce the same output.

func Encode

func Encode(val int64) string

Encode formats bytes integer to human-readable string according to IEC 60027.

For example, 31323 bytes will return "30.59 KB".

The smallest supported value is "1 B", so precision will be ignored below "1 KiB" or "1 KB".

func EncodeBinary

func EncodeBinary(val int64) string

EncodeBinary formats bytes integer to human-readable string according to IEC 60027.

For example, 31323 bytes will return "30.59 KB".

The smallest supported value is "1 B", so precision will be ignored below "1 KB".

func EncodeDecimal

func EncodeDecimal(val int64) string

EncodeDecimal formats bytes integer to human-readable string according to SI international system of units.

For example, 31323 bytes will return "31.32 KB".

The smallest supported value is "1 B", so precision will be ignored below "1 KiB".

Types

type Decoder

type Decoder struct{}

A Decoder decodes strings to byte values.

func NewDecoder

func NewDecoder() *Decoder

NewDecoder creates a new Decoder.

func (*Decoder) Decode

func (b *Decoder) Decode(text string) (int64, error)

Decode parses human-readable bytes string to bytes integer.

For example, "6 GiB" ("6 Gi" is also valid) will return 6442450944, and "6 GB" ("6 G" is also valid) will return 6000000000. The input case-insensitive, and the space is optional, so "6GiB" or "6GB" would produce the same output.

func (*Decoder) DecodeBinary

func (b *Decoder) DecodeBinary(text string) (int64, error)

DecodeBinary parses human-readable bytes string to bytes integer.

For example, "6 GiB" ("6 Gi" is also valid) will return 6442450944. The input case-insensitive, and the space is optional, so "6GiB" would produce the same output.

func (*Decoder) DecodeDecimal

func (b *Decoder) DecodeDecimal(text string) (int64, error)

DecodeDecimal parses human-readable bytes string to bytes integer.

For example, "6 GB" ("6 G" is also valid) will return 6000000000. The input case-insensitive, and the space is optional, so "6GB" would produce the same output.

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}

A Encoder encodes byte values to strings.

func NewEncoder

func NewEncoder() *Encoder

NewEncoder creates a new Encoder.

func (*Encoder) Encode

func (b *Encoder) Encode(val int64) string

Encode formats bytes integer to human-readable string according to IEC 60027.

For example, 31323 bytes will return "30.59 KB".

The smallest supported value is "1 B", so precision will be ignored below "1 KiB" or "1 KB".

func (*Encoder) EncodeBinary

func (b *Encoder) EncodeBinary(valInt int64) string

EncodeBinary formats bytes integer to human-readable string according to IEC 60027.

For example, 31323 bytes will return "30.59 KB".

The smallest supported value is "1 B", so precision will be ignored below "1 KB".

func (*Encoder) EncodeDecimal

func (b *Encoder) EncodeDecimal(valInt int64) string

EncodeDecimal formats bytes integer to human-readable string according to SI international system of units.

For example, 31323 bytes will return "31.32 KB".

The smallest supported value is "1 B", so precision will be ignored below "1 KiB".

func (*Encoder) SetPrecision

func (b *Encoder) SetPrecision(precision int) *Encoder

SetPrecision instructs the encoder to include the given number of decimal places.

The special precision -1 uses the smallest number of digits necessary.

func (*Encoder) SetTrimIntDecimal

func (b *Encoder) SetTrimIntDecimal(trimIntZero bool) *Encoder

SetTrimIntDecimal instructs the encoder to remove the decimal if the result is an integer (ends with ".00").

func (*Encoder) SetUseSpace

func (b *Encoder) SetUseSpace(useSpace bool) *Encoder

SetUseSpace instructs the encoder to separate the value and suffix with a space.

Jump to

Keyboard shortcuts

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