Categorygithub.com/dustin/go-humanize
modulepackage
1.0.1
Repository: https://github.com/dustin/go-humanize.git
Documentation: pkg.go.dev

# README

Humane Units Build Status GoDoc

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.

See godoc for complete documentation.

Sizes

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

Example:

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

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)) // This was touched 7 hours ago.

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)) // You are my 193rd best friend.

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)) // You owe $6,582,491.

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.23 nM

English-specific functions

The following functions are in the humanize/english subpackage.

Plurals

Simple English pluralization

english.PluralWord(1, "object", "") // object
english.PluralWord(42, "object", "") // objects
english.PluralWord(2, "bus", "") // buses
english.PluralWord(99, "locus", "loci") // loci

english.Plural(1, "object", "") // 1 object
english.Plural(42, "object", "") // 42 objects
english.Plural(2, "bus", "") // 2 buses
english.Plural(99, "locus", "loci") // 99 loci

Word series

Format comma-separated words lists with conjuctions:

english.WordSeries([]string{"foo"}, "and") // foo
english.WordSeries([]string{"foo", "bar"}, "and") // foo and bar
english.WordSeries([]string{"foo", "bar", "baz"}, "and") // foo, bar and baz

english.OxfordWordSeries([]string{"foo", "bar", "baz"}, "and") // foo, bar, and baz

# Packages

Package english provides utilities to generate more user-friendly English output.

# 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.
BigCommaf produces a string form of the given big.Float 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.
Commaf produces a string form of the given number in base 10 with commas after every three orders of magnitude.
CommafWithDigits works like the Commaf but limits the resulting string to the given number of decimal places.
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.
CustomRelTime formats a time into a relative string.
FormatFloat produces a formatted number as string based on the following user-specified criteria: * thousands separator * decimal separator * decimal precision Usage: s := RenderFloat(format, n) The format parameter tells how to render the number n.
FormatInteger produces a formatted number as string.
Ftoa converts a float to a string with no trailing zeros.
FtoaWithDigits converts a float to a string but limits the resulting string to the given number of decimal places, and 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.
RelTime formats a time into a relative string.
SI returns a string with default formatting.
SIWithDigits works like SI but limits the resulting string to the given number of decimal places.
Time formats a time into a relative string.

# Constants

IEC Sizes.
Seconds-based time units.
SI Sizes.
IEC Sizes.
SI Sizes.
IEC Sizes.
SI Sizes.
SI Sizes.
IEC Sizes.
Seconds-based time units.
SI Sizes.
IEC Sizes.
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.
BigQByte is 1,000 SI r bytes in big.Ints.
BigQiByte is 1,024 r bytes in bit.Ints.
BigRByte is 1,000 SI y bytes in big.Ints.
BigRiByte is 1,024 y 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.

# Structs

A RelTimeMagnitude struct contains a relative time point at which the relative format of time will switch to a new format string.