diff --git a/cmd/badges/main.go b/cmd/badges/main.go index dfc6567f..224e4016 100644 --- a/cmd/badges/main.go +++ b/cmd/badges/main.go @@ -18,7 +18,7 @@ func main() { //pageSize := 20 title := "Total lines" text_length := "250" - s := "1.5k" + s := formatCount(30000) w.Header().Set("Content-Type", "image/svg+xml;charset=utf-8") w.Write([]byte(` ` + title + `` + title + `` + s + `` + s + ` `)) @@ -78,7 +78,12 @@ func formatCount(count float64) string { for _, v := range ranges { if count >= v.val { - t := math.Round(count / v.val) + t := fmt.Sprintf("%.1f", math.Round(count/v.val)) + + if len(t) > 3 { + t = t[:strings.Index(t, ".")] + } + return fmt.Sprintf("%v%v", t, v.sym) } } diff --git a/cmd/badges/main_test.go b/cmd/badges/main_test.go index 551da355..d4a99d2e 100644 --- a/cmd/badges/main_test.go +++ b/cmd/badges/main_test.go @@ -1,6 +1,10 @@ package main -import "testing" +import ( + "fmt" + "math" + "testing" +) func Test_processPath(t *testing.T) { type args struct { @@ -28,6 +32,9 @@ func Test_processPath(t *testing.T) { } func Test_formatCount(t *testing.T) { + + fmt.Println(math.Round(2500 / 1000)) + type args struct { count float64 } @@ -57,6 +64,34 @@ func Test_formatCount(t *testing.T) { }, want: "2.5k", }, + { + name: "", + args: args{ + count: 436465, + }, + want: "436k", + }, + { + name: "", + args: args{ + count: 263804, + }, + want: "264k", + }, + { + name: "", + args: args{ + count: 86400, + }, + want: "86k", + }, + { + name: "", + args: args{ + count: 81.99825581739397, + }, + want: "82", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {