format

package
v0.0.0-...-8c65170 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2024 License: Apache-2.0 Imports: 7 Imported by: 22

Documentation

Overview

Package format provides utility methods for formatting units.

Index

Constants

View Source
const (
	Celsius temperatureUnit = iota
	Fahrenheit
	Kelvin
)

Pass in these constants to SetTemperatureUnit to control the default format.

Variables

This section is empty.

Functions

func Byterate

func Byterate(v unit.Datarate) string

Byterate formats a Datarate in SI units using go-humanize. e.g. Byterate(10 * unit.MegabytePerSecond) == "10 MB/s"

func Bytesize

func Bytesize(v unit.Datasize) string

Bytesize formats a Datasize in SI units using go-humanize. e.g. Bytesize(10 * unit.Megabyte) == "10 MB"

func IByterate

func IByterate(v unit.Datarate) string

IByterate formats a Datarate in IEC units using go-humanize. e.g. Byterate(10 * unit.MebibytePerSecond) == "10 MiB/s"

func IBytesize

func IBytesize(v unit.Datasize) string

IBytesize formats a Datasize in IEC units using go-humanize. e.g. IBytesize(10 * unit.Mebibyte) == "10 MiB"

func SetTemperatureUnit

func SetTemperatureUnit(f temperatureUnit)

SetTemperatureUnit sets the default unit used when formatting temperatures.

Types

type Value

type Value struct {
	Unit string
	// contains filtered or unexported fields
}

Value represents a formatted Unit value.

func SI

func SI(v float64, unit string) Value

SI formats an SI unit value by scaling it to a sensible multiplier, and returns a three-character value (four if negative) and a suffix that's either empty or a single character. e.g. format.SI((20480*unit.Megabyte).Bytes(), "B") == {"20.48000", "GB"} or format.SI((0.001234*unit.Foot).Meter(), "m") == {"376.1232", "µm"}

func SIUnit

func SIUnit(val interface{}) (Value, bool)

SIUnit formats a unit.Unit value to an appropriately scaled base unit. For example, SIUnit(length) is equivalent to SI(length.Meters(), "m"). For non-base units (e.g. feet), use SI(length.Feet(), "ft").

func (Value) Number

func (v Value) Number(width int) string

Number returns a representation that occupies at least `width` characters, increasing precision to fill the available space.

func (Value) String

func (v Value) String() string

String formats the value as a string

func (Value) StringW

func (v Value) StringW(w int) string

StringW formats the value as a string, using the given width for the numeric portion.

type Values

type Values []Value

Values represents an ordered list of values, e.g. hours/minutes/seconds.

func Duration

func Duration(d time.Duration) Values

Duration formats a time.Duration by providing the two most significant units.

func Unit

func Unit(value interface{}) (Values, bool)

Unit formats a unit.Unit value to the most appropriately scaled base unit. For example, Unit(length) is equivalent to SI(length.Meters(), "m"). For non-base units (e.g. feet), use SI(length.Feet(), "ft").

func (Values) String

func (v Values) String() string

String formats the values as a string

Jump to

Keyboard shortcuts

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