You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.0 KiB

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