Documentation ¶
Overview ¶
Package humanize converts boring ugly numbers to human-friendly strings and back.
Durations can be turned into strings such as "3 days ago", numbers representing sizes like 82854982 into useful strings like, "83MB" or "79MiB" (whichever you prefer).
Index ¶
- Constants
- Variables
- func BigBytes(s *big.Int) string
- func BigComma(b *big.Int) string
- func BigIBytes(s *big.Int) string
- func Bytes(s uint64) string
- func Comma(v int64) string
- func Commaf(v float64) string
- func ComputeSI(input float64) (float64, string)
- func Ftoa(num float64) string
- func IBytes(s uint64) string
- func Ordinal(x int) string
- func ParseBigBytes(s string) (*big.Int, error)
- func ParseBytes(s string) (uint64, error)
- func ParseSI(input string) (float64, string, error)
- func RelTime(a, b time.Time, albl, blbl string) string
- func SI(input float64, unit string) string
- func Time(then time.Time) string
Constants ¶
const ( Byte = 1 << (iota * 10) KiByte MiByte GiByte TiByte PiByte EiByte )
IEC Sizes. kibis of bits
const ( IByte = 1 KByte = IByte * 1000 MByte = KByte * 1000 GByte = MByte * 1000 TByte = GByte * 1000 PByte = TByte * 1000 EByte = PByte * 1000 )
SI Sizes.
const ( Minute = 60 Hour = 60 * Minute Day = 24 * Hour Week = 7 * Day Month = 30 * Day Year = 12 * Month LongTime = 37 * Year )
Seconds-based time units
Variables ¶
var ( // BigByte is one byte in bit.Ints BigByte = big.NewInt(1) // BigKiByte is 1,024 bytes in bit.Ints BigKiByte = (&big.Int{}).Mul(BigByte, bigIECExp) // BigMiByte is 1,024 k bytes in bit.Ints BigMiByte = (&big.Int{}).Mul(BigKiByte, bigIECExp) // BigGiByte is 1,024 m bytes in bit.Ints BigGiByte = (&big.Int{}).Mul(BigMiByte, bigIECExp) // BigTiByte is 1,024 g bytes in bit.Ints BigTiByte = (&big.Int{}).Mul(BigGiByte, bigIECExp) // BigPiByte is 1,024 t bytes in bit.Ints BigPiByte = (&big.Int{}).Mul(BigTiByte, bigIECExp) // BigEiByte is 1,024 p bytes in bit.Ints BigEiByte = (&big.Int{}).Mul(BigPiByte, bigIECExp) // BigZiByte is 1,024 e bytes in bit.Ints BigZiByte = (&big.Int{}).Mul(BigEiByte, bigIECExp) // BigYiByte is 1,024 z bytes in bit.Ints BigYiByte = (&big.Int{}).Mul(BigZiByte, bigIECExp) )
var ( // BigSIByte is one SI byte in big.Ints BigSIByte = big.NewInt(1) // BigKByte is 1,000 SI bytes in big.Ints BigKByte = (&big.Int{}).Mul(BigSIByte, bigSIExp) // BigMByte is 1,000 SI k bytes in big.Ints BigMByte = (&big.Int{}).Mul(BigKByte, bigSIExp) // BigGByte is 1,000 SI m bytes in big.Ints BigGByte = (&big.Int{}).Mul(BigMByte, bigSIExp) // BigTByte is 1,000 SI g bytes in big.Ints BigTByte = (&big.Int{}).Mul(BigGByte, bigSIExp) // BigPByte is 1,000 SI t bytes in big.Ints BigPByte = (&big.Int{}).Mul(BigTByte, bigSIExp) // BigEByte is 1,000 SI p bytes in big.Ints BigEByte = (&big.Int{}).Mul(BigPByte, bigSIExp) // BigZByte is 1,000 SI e bytes in big.Ints BigZByte = (&big.Int{}).Mul(BigEByte, bigSIExp) // BigYByte is 1,000 SI z bytes in big.Ints BigYByte = (&big.Int{}).Mul(BigZByte, bigSIExp) )
Functions ¶
func BigBytes ¶
BigBytes produces a human readable representation of an SI size.
See also: ParseBigBytes.
BigBytes(82854982) -> 83MB
func BigComma ¶
BigComma produces a string form of the given big.Int in base 10 with commas after every three orders of magnitude.
func BigIBytes ¶
BigIBytes produces a human readable representation of an IEC size.
See also: ParseBigBytes.
BigIBytes(82854982) -> 79MiB
func Bytes ¶
Bytes produces a human readable representation of an SI size.
See also: ParseBytes.
Bytes(82854982) -> 83MB
func Comma ¶
Comma produces a string form of the given number in base 10 with commas after every three orders of magnitude.
e.g. Comma(834142) -> 834,142
func Commaf ¶
Commaf produces a string form of the given number in base 10 with commas after every three orders of magnitude.
e.g. Comma(834142.32) -> 834,142.32
func ComputeSI ¶
ComputeSI finds the most appropriate SI prefix for the given number and returns the prefix along with the value adjusted to be within that prefix.
See also: SI, ParseSI.
e.g. ComputeSI(2.2345e-12) -> (2.2345, "p")
func IBytes ¶
IBytes produces a human readable representation of an IEC size.
See also: ParseBytes.
IBytes(82854982) -> 79MiB
func ParseBigBytes ¶
ParseBigBytes parses a string representation of bytes into the number of bytes it represents.
See also: BigBytes, BigIBytes.
ParseBigBytes("42MB") -> 42000000, nil ParseBigBytes("42mib") -> 44040192, nil
func ParseBytes ¶
ParseBytes parses a string representation of bytes into the number of bytes it represents.
See Also: Bytes, IBytes.
ParseBytes("42MB") -> 42000000, nil ParseBytes("42mib") -> 44040192, nil
func ParseSI ¶
ParseSI parses an SI string back into the number and unit.
See also: SI, ComputeSI.
e.g. ParseSI(2.2345pF) -> (2.2345e-12, "F", nil)
func RelTime ¶
RelTime formats a time into a relative string.
It takes two times and two labels. In addition to the generic time delta string (e.g. 5 minutes), the labels are used applied so that the label corresponding to the smaller time is applied.
RelTime(timeInPast, timeInFuture, "earlier", "later") -> "3 weeks earlier"
func SI ¶
SI returns a string with default formatting.
SI uses Ftoa to format float value, removing trailing zeros.
See also: ComputeSI, ParseSI.
e.g. SI(1000000, B) -> 1MB e.g. SI(2.2345e-12, "F") -> 2.2345pF
Types ¶
This section is empty.