blob: 91b4ae56464b6ab79556c8869da84273a252656a [file] [log] [blame]
khenaidooab1f7bd2019-11-14 14:00:27 -05001# Humane Units [![Build Status](https://travis-ci.org/dustin/go-humanize.svg?branch=master)](https://travis-ci.org/dustin/go-humanize) [![GoDoc](https://godoc.org/github.com/dustin/go-humanize?status.svg)](https://godoc.org/github.com/dustin/go-humanize)
2
3Just a few functions for helping humanize times and sizes.
4
5`go get` it as `github.com/dustin/go-humanize`, import it as
6`"github.com/dustin/go-humanize"`, use it as `humanize`.
7
8See [godoc](https://godoc.org/github.com/dustin/go-humanize) for
9complete documentation.
10
11## Sizes
12
13This lets you take numbers like `82854982` and convert them to useful
14strings like, `83 MB` or `79 MiB` (whichever you prefer).
15
16Example:
17
18```go
19fmt.Printf("That file is %s.", humanize.Bytes(82854982)) // That file is 83 MB.
20```
21
22## Times
23
24This lets you take a `time.Time` and spit it out in relative terms.
25For example, `12 seconds ago` or `3 days from now`.
26
27Example:
28
29```go
30fmt.Printf("This was touched %s.", humanize.Time(someTimeInstance)) // This was touched 7 hours ago.
31```
32
33Thanks to Kyle Lemons for the time implementation from an IRC
34conversation one day. It's pretty neat.
35
36## Ordinals
37
38From a [mailing list discussion][odisc] where a user wanted to be able
39to label ordinals.
40
41 0 -> 0th
42 1 -> 1st
43 2 -> 2nd
44 3 -> 3rd
45 4 -> 4th
46 [...]
47
48Example:
49
50```go
51fmt.Printf("You're my %s best friend.", humanize.Ordinal(193)) // You are my 193rd best friend.
52```
53
54## Commas
55
56Want to shove commas into numbers? Be my guest.
57
58 0 -> 0
59 100 -> 100
60 1000 -> 1,000
61 1000000000 -> 1,000,000,000
62 -100000 -> -100,000
63
64Example:
65
66```go
67fmt.Printf("You owe $%s.\n", humanize.Comma(6582491)) // You owe $6,582,491.
68```
69
70## Ftoa
71
72Nicer float64 formatter that removes trailing zeros.
73
74```go
75fmt.Printf("%f", 2.24) // 2.240000
76fmt.Printf("%s", humanize.Ftoa(2.24)) // 2.24
77fmt.Printf("%f", 2.0) // 2.000000
78fmt.Printf("%s", humanize.Ftoa(2.0)) // 2
79```
80
81## SI notation
82
83Format numbers with [SI notation][sinotation].
84
85Example:
86
87```go
88humanize.SI(0.00000000223, "M") // 2.23 nM
89```
90
91## English-specific functions
92
93The following functions are in the `humanize/english` subpackage.
94
95### Plurals
96
97Simple English pluralization
98
99```go
100english.PluralWord(1, "object", "") // object
101english.PluralWord(42, "object", "") // objects
102english.PluralWord(2, "bus", "") // buses
103english.PluralWord(99, "locus", "loci") // loci
104
105english.Plural(1, "object", "") // 1 object
106english.Plural(42, "object", "") // 42 objects
107english.Plural(2, "bus", "") // 2 buses
108english.Plural(99, "locus", "loci") // 99 loci
109```
110
111### Word series
112
113Format comma-separated words lists with conjuctions:
114
115```go
116english.WordSeries([]string{"foo"}, "and") // foo
117english.WordSeries([]string{"foo", "bar"}, "and") // foo and bar
118english.WordSeries([]string{"foo", "bar", "baz"}, "and") // foo, bar and baz
119
120english.OxfordWordSeries([]string{"foo", "bar", "baz"}, "and") // foo, bar, and baz
121```
122
123[odisc]: https://groups.google.com/d/topic/golang-nuts/l8NhI74jl-4/discussion
124[sinotation]: http://en.wikipedia.org/wiki/Metric_prefix