Categorygithub.com/flosch/go-humanize
modulepackage
0.0.0-20140728123800-3ba51eabe506
Repository: https://github.com/flosch/go-humanize.git
Documentation: pkg.go.dev

# README

Humane Units

Just a few functions for helping humanize times and sizes.

go get it as github.com/dustin/go-humanize, import it as "github.com/dustin/go-humanize", use it as humanize

Sizes

This lets you take numbers like 82854982 and convert them to useful strings like, 83MB or 79MiB (whichever you prefer).

Example:

fmt.Printf("That file is %s.", humanize.Bytes(82854982))

Times

This lets you take a time.Time and spit it out in relative terms. For example, 12 seconds ago or 3 days from now.

Example:

fmt.Printf("This was touched %s", humanize.Time(someTimeInstance))

Thanks to Kyle Lemons for the time implementation from an IRC conversation one day. It's pretty neat.

Ordinals

From a mailing list discussion where a user wanted to be able to label ordinals.

0 -> 0th
1 -> 1st
2 -> 2nd
3 -> 3rd
4 -> 4th
[...]

Example:

fmt.Printf("You're my %s best friend.", humanize.Ordinal(193))

Commas

Want to shove commas into numbers? Be my guest.

0 -> 0
100 -> 100
1000 -> 1,000
1000000000 -> 1,000,000,000
-100000 -> -100,000

Example:

fmt.Printf("You owe $%s.\n", humanize.Comma(6582491))

Ftoa

Nicer float64 formatter that removes trailing zeros.

fmt.Printf("%f", 2.24)                   // 2.240000
fmt.Printf("%s", humanize.Ftoa(2.24))    // 2.24
fmt.Printf("%f", 2.0)                    // 2.000000
fmt.Printf("%s", humanize.Ftoa(2.0))     // 2

SI notation

Format numbers with SI notation.

Example:

humanize.SI(0.00000000223, "M")    // 2.23nM

# Functions

BigBytes produces a human readable representation of an SI size.
BigComma produces a string form of the given big.Int in base 10 with commas after every three orders of magnitude.
BigIBytes produces a human readable representation of an IEC size.
Bytes produces a human readable representation of an SI size.
Comma produces a string form of the given number in base 10 with commas after every three orders of magnitude.
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.
Ftoa converts a float to a string with no trailing zeros.
IBytes produces a human readable representation of an IEC size.
Ordinal gives you the input number in a rank/ordinal format.
ParseBigBytes parses a string representation of bytes into the number of bytes it represents.
ParseBytes parses a string representation of bytes into the number of bytes it represents.
ParseSI parses an SI string back into the number and unit.
SI returns a string with default formatting.
Time formats a time into a relative string.
No description provided by the author

# Constants

IEC Sizes.
Seconds-based time units.
SI Sizes.
IEC Sizes.
SI Sizes.
IEC Sizes.
Seconds-based time units.
SI Sizes.
SI Sizes.
IEC Sizes.
Seconds-based time units.
SI Sizes.
IEC Sizes.
Seconds-based time units.
Seconds-based time units.
SI Sizes.
IEC Sizes.
SI Sizes.
IEC Sizes.
Seconds-based time units.
Seconds-based time units.

# Variables

BigByte is one byte in bit.Ints.
BigEByte is 1,000 SI p bytes in big.Ints.
BigEiByte is 1,024 p bytes in bit.Ints.
BigGByte is 1,000 SI m bytes in big.Ints.
BigGiByte is 1,024 m bytes in bit.Ints.
BigKByte is 1,000 SI bytes in big.Ints.
BigKiByte is 1,024 bytes in bit.Ints.
BigMByte is 1,000 SI k bytes in big.Ints.
BigMiByte is 1,024 k bytes in bit.Ints.
BigPByte is 1,000 SI t bytes in big.Ints.
BigPiByte is 1,024 t bytes in bit.Ints.
BigSIByte is one SI byte in big.Ints.
BigTByte is 1,000 SI g bytes in big.Ints.
BigTiByte is 1,024 g bytes in bit.Ints.
BigYByte is 1,000 SI z bytes in big.Ints.
BigYiByte is 1,024 z bytes in bit.Ints.
BigZByte is 1,000 SI e bytes in big.Ints.
BigZiByte is 1,024 e bytes in bit.Ints.