or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

byte-sizes.mdenglish.mdindex.mdnumber-formatting.mdordinals.mdsi-notation.mdtime-formatting.md
tile.json

byte-sizes.mddocs/

Byte Size Formatting

Converts numeric byte values to human-readable strings and parses string representations back to numeric values. Supports both SI decimal units (KB, MB, GB using powers of 1000) and IEC binary units (KiB, MiB, GiB using powers of 1024).

Import

import "github.com/dustin/go-humanize"

Standard Size Formatting (uint64)

Bytes

func Bytes(s uint64) string

Formats a byte count as a human-readable string using SI units (powers of 1000).

Parameters:

  • s - The size in bytes

Returns: Human-readable string like "83 MB", "1.2 GB"

Example:

humanize.Bytes(82854982)        // "83 MB"
humanize.Bytes(1024)            // "1.0 KB"
humanize.Bytes(1000000000)      // "1.0 GB"

IBytes

func IBytes(s uint64) string

Formats a byte count as a human-readable string using IEC units (powers of 1024).

Parameters:

  • s - The size in bytes

Returns: Human-readable string like "79 MiB", "1.0 GiB"

Example:

humanize.IBytes(82854982)       // "79 MiB"
humanize.IBytes(1024)           // "1.0 KiB"
humanize.IBytes(1073741824)     // "1.0 GiB"

ParseBytes

func ParseBytes(s string) (uint64, error)

Parses a string representation of bytes into a uint64 value. Supports both SI and IEC units.

Parameters:

  • s - String like "42 MB", "42 mib", "1.5 GB"

Returns:

  • uint64 - The parsed byte count
  • error - Error if parsing fails

Example:

bytes, err := humanize.ParseBytes("42 MB")   // 42000000, nil
bytes, err := humanize.ParseBytes("42 MiB")  // 44040192, nil
bytes, err := humanize.ParseBytes("1.5 GB")  // 1500000000, nil

Big Integer Size Formatting

For sizes that exceed uint64 capacity, the package provides equivalent functions using big.Int.

BigBytes

func BigBytes(s *big.Int) string

Formats a big.Int byte count as a human-readable string using SI units.

Parameters:

  • s - The size in bytes as a big.Int

Returns: Human-readable string like "83 MB"

Example:

import "math/big"

size := big.NewInt(82854982)
humanize.BigBytes(size)  // "83 MB"

BigIBytes

func BigIBytes(s *big.Int) string

Formats a big.Int byte count as a human-readable string using IEC units.

Parameters:

  • s - The size in bytes as a big.Int

Returns: Human-readable string like "79 MiB"

Example:

import "math/big"

size := big.NewInt(82854982)
humanize.BigIBytes(size)  // "79 MiB"

ParseBigBytes

func ParseBigBytes(s string) (*big.Int, error)

Parses a string representation of bytes into a big.Int value. Supports both SI and IEC units.

Parameters:

  • s - String like "42 MB", "42 mib", "999 ZB"

Returns:

  • *big.Int - The parsed byte count
  • error - Error if parsing fails

Example:

bytes, err := humanize.ParseBigBytes("42 MB")   // big.Int(42000000), nil
bytes, err := humanize.ParseBigBytes("42 MiB")  // big.Int(44040192), nil

Size Constants

The package exports constants for common byte sizes:

// IEC binary size constants (powers of 1024)
const (
	Byte = 1 << (iota * 10)  // 1
	KiByte                    // 1024
	MiByte                    // 1048576
	GiByte                    // 1073741824
	TiByte
	PiByte
	EiByte
)

// SI decimal size constants (powers of 1000)
const (
	IByte = 1
	KByte = IByte * 1000      // 1000
	MByte = KByte * 1000      // 1000000
	GByte = MByte * 1000      // 1000000000
	TByte = GByte * 1000
	PByte = TByte * 1000
	EByte = PByte * 1000
)

Big.Int Size Variables

For very large sizes, the package provides pre-computed big.Int variables:

// IEC binary size variables (powers of 1024)
var (
	BigByte   *big.Int  // 1
	BigKiByte *big.Int  // 1024
	BigMiByte *big.Int  // 1024^2
	BigGiByte *big.Int  // 1024^3
	BigTiByte *big.Int
	BigPiByte *big.Int
	BigEiByte *big.Int
	BigZiByte *big.Int
	BigYiByte *big.Int
	BigRiByte *big.Int
	BigQiByte *big.Int
)

// SI decimal size variables (powers of 1000)
var (
	BigSIByte *big.Int  // 1
	BigKByte  *big.Int  // 1000
	BigMByte  *big.Int  // 1000^2
	BigGByte  *big.Int  // 1000^3
	BigTByte  *big.Int
	BigPByte  *big.Int
	BigEByte  *big.Int
	BigZByte  *big.Int
	BigYByte  *big.Int
	BigRByte  *big.Int
	BigQByte  *big.Int
)

Example:

import "math/big"

// Calculate a very large size
largeSize := (&big.Int{}).Mul(humanize.BigGByte, big.NewInt(9999999))
fmt.Println(humanize.BigBytes(largeSize))  // "10 PB"

Notes

  • SI vs IEC: SI units (KB, MB, GB) use powers of 1000 and are standard for disk drive manufacturers. IEC units (KiB, MiB, GiB) use powers of 1024 and are standard in computing contexts.
  • Parsing: ParseBytes and ParseBigBytes are case-insensitive and accept both "MB" and "mb" or "MiB" and "mib".
  • Precision: Formatted strings typically show 2-3 significant figures with one decimal place.