Skip to content
/ humanize Public

(Exercise) Package humanize provides functions to convert number to various human readable format.

License

Notifications You must be signed in to change notification settings

tdsh/humanize

Repository files navigation

humanize

import "github.com/tdsh/humanize"

(Exercise) Package humanize provides functions to convert number to various human readable format.

filesize.go i18n.go number.go time.go

func APNumber(n int) string

APNumber returns the number spelled out for 1-9. Otherwise, just returns the number as string.

func FormatPercent(f float64) string

FormatPercent converts float64 value f to a percentage. It can accept value more than 1 or negative.

func Fractional(f float64) string

Fractional converts float64 value f to fractinal number as a string, in forms of fractions and mixed fractions.

func IntComma(n int) string

IntComma converts the integer n to a string containing commas every three digits. It accepts octal or hex number. Returned value is decimal in those cases too. If the integer is octal or hex, decimal is also returned.

func IntWord(n int64) string

IntWord converts a large integer n to a friendly text representation. For example, 1000000 becomes '1.0 million', and '1200000000' becomes '1.2 billion'. If n is negative, the number is just returned as string. It accepts octal or hex number. Returned value is decimal in those cases too.

func NaturalDay(t time.Time) string

NaturalDay compares t to present day and returns tomorrow, today or yesterday if applicable. Otherwise, it returns date as a string.

func NaturalDelta(t time.Time, relative bool) string

NaturalDelta computes the natural representation of the amount of time elapsed from now. If relative is true, "ago" or "from now" is also returned depending on whether t is past or future.

func NaturalSize(val int, options ...SetOption) string

NaturalSize formats val as a number of byteslike a human readable filesize (eg. 10 kB). Returns decimal suffixes (kB, MB...) by default. If option Binary(true) is given, binary suffixes (KiB, MiB...) are used and the base will be 210 instead of 103. If option GNU(true) is given, GNU-style prefixes (K, M...) are used. GNU had higher priority than Binary so if both options are true, Binary is ignored.

Here's some examples to call NaturalSize:

NaturalSize(123)
NaturalSize(1978, GNU(true))
NaturalSize(1000000000, Binary(true))

Passing both GNU and Binary options is allowed. But in this case GNU is given priority and Binary is ignored.

NaturalSize(742617000027, GNU(true), Binary(true))
func Ordinal(n int) string

Ordinal converts the integer n to the ordinal number as string.

func SetLocale(l string)

SetLocale sets l to current locale. l must be specified in "lang code (ISO 639)"-"country code (ISO 3166-1)". (en-us, ja-jp, for example.) Available translations are located at translations directory as JSON file. If unsupported language is selected, English (en-us) is used.

type FormatOption struct {
    // contains filtered or unexported fields
}

FormatOption is the options NaturalSize can take.

type SetOption func(*FormatOption)

SetOption is method definition to set FormatOption.

func Binary(on bool) SetOption

Binary enables binary suffixes (KiB, MiB...) for NaturalSize function. It also sets the base 210 instead of 103.

func GNU(on bool) SetOption

GNU enables GNU-style prefixes (K, M...) for NaturalSize function.


Generated by godoc2md

About

(Exercise) Package humanize provides functions to convert number to various human readable format.

Resources

License

Stars

Watchers

Forks

Packages

No packages published