convert

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2026 License: GPL-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package convert provides function to convert and humanize units

Index

Examples

Constants

View Source
const IECBase = 1024

IECBase is the exponential base for IEC units.

View Source
const SIBase = 1000

SIBase is the exponential base for SI units.

Variables

View Source
var ByteIECUnits = []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB"}

ByteIECUnits lists known units we can convert to based on uint64.

View Source
var ByteSIUnits = []string{"B", "KB", "MB", "GB", "TB", "PB"}

ByteSIUnits lists known units we can convert to based on uint64.

Functions

func BytesIEC added in v0.3.0

func BytesIEC(b uint64) string

BytesIEC returns the biggest sensible unit for the byte value with 2 decimal precision. When value is smaller than 2 render it with a lower scale.

Example
b := BytesIEC(999 * 1024)

fmt.Println(b)
Output:
999KiB

func BytesSI added in v0.3.0

func BytesSI(b uint64) string

BytesSI returns the biggest sensible unit for the byte value with 2 decimal precision. When value is smaller than 2 render it with a lower scale.

Example
b := BytesSI(999 * 1000)

fmt.Println(b)
Output:
999KB

func ParseBytes

func ParseBytes(value string) (uint64, error)

ParseBytes parses a strings and returns the number of bytes

Example
b, err := ParseBytes("999KiB")

if err != nil {
	panic("Could not parse string into number of bytes")
}

fmt.Println(b)
Output:
1022976

Types

This section is empty.

Jump to

Keyboard shortcuts

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