humanbytes

package module
v0.0.0-...-d58fbfd Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2025 License: Apache-2.0 Imports: 2 Imported by: 2

README

zzz-humanbytes

Demo of converting bytes to human readable string with ls -lh semantics, as discussed in the article Human-readable bytes like 'ls -lh' in Go.

Copyright 2022 by David A. Golden. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"). You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

Documentation

Overview

Package humanbytes converts bytes to human-readable strings with ls -lh semantics. For bytes >= 1 KiB/kB, human-readable strings are in the form "X.Y <suffix>" where X and Y are single digits, or "Z <suffix>" where Z is two or three digits.

All sizes are aggressively rounded up:

  • 1025 rounds to 1.1 K
  • 9.9 * 1024**2 + 1 rounds to 10 M

Base-2 and Base-10 are supported, including modern Base-2 suffixes.

Additional Info

ls -lh has special, irregular semantics I couldn’t find all implemented together in the same library!

  • Use no decimal pt or unit suffix for values under 1024 bytes: 0 or 897
  • Use one decimal if the integer part is a single digit: 1.0 K or 9.9 M
  • Use no decimal if the integer part is more than one digit: 10 K or 582 M
  • Aggressively round up: 999 M + 1 byte rounds up to 1 G

.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SizeIEC

func SizeIEC(size int) string

func SizeLS

func SizeLS(size int) string

func SizeSI

func SizeSI(size int) string

Types

This section is empty.

Jump to

Keyboard shortcuts

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