package humanizeimport ()// Comma produces a string form of the given number in base 10 with// commas after every three orders of magnitude.//// e.g. Comma(834142) -> 834,142func ( int64) string { := ""// Min int64 can't be negated to a usable value, so it has to be special cased.if == math.MinInt64 {return"-9,223,372,036,854,775,808" }if < 0 { = "-" = 0 - } := []string{"", "", "", "", "", "", ""} := len() - 1for > 999 { [] = strconv.FormatInt(%1000, 10)switchlen([]) {case2: [] = "0" + []case1: [] = "00" + [] } = / 1000 -- } [] = strconv.Itoa(int())return + strings.Join([:], ",")}// Commaf produces a string form of the given number in base 10 with// commas after every three orders of magnitude.//// e.g. Commaf(834142.32) -> 834,142.32func ( float64) string { := &bytes.Buffer{}if < 0 { .Write([]byte{'-'}) = 0 - } := []byte{','} := strings.Split(strconv.FormatFloat(, 'f', -1, 64), ".") := 0iflen([0])%3 != 0 { += len([0]) % 3 .WriteString([0][:]) .Write() }for ; < len([0]); += 3 { .WriteString([0][ : +3]) .Write() } .Truncate(.Len() - 1)iflen() > 1 { .Write([]byte{'.'}) .WriteString([1]) }return .String()}// CommafWithDigits works like the Commaf but limits the resulting// string to the given number of decimal places.//// e.g. CommafWithDigits(834142.32, 1) -> 834,142.3func ( float64, int) string {returnstripTrailingDigits(Commaf(), )}// BigComma produces a string form of the given big.Int in base 10// with commas after every three orders of magnitude.func ( *big.Int) string { := ""if .Sign() < 0 { = "-" .Abs() } := big.NewInt(1000) := (&big.Int{}).Set() , := oom(, ) := make([]string, +1) := len() - 1 := &big.Int{}for .Cmp() >= 0 { .DivMod(, , ) [] = strconv.FormatInt(.Int64(), 10)switchlen([]) {case2: [] = "0" + []case1: [] = "00" + [] } -- } [] = strconv.Itoa(int(.Int64()))return + strings.Join([:], ",")}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.