Skip to content

Commit

Permalink
float numbertoK only show decimal when has one
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincobain2000 committed Jan 5, 2024
1 parent 1dd03e7 commit 4b83f6f
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions pkg/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,22 @@ func NumberToK(num *float64) string {
return "0"
}

formatNumber := func(n float64) string {
if n == float64(int64(n)) {
// If n is an integer, format without decimal places.
return strconv.FormatFloat(n, 'f', 0, 64)
}
// Otherwise, format with one decimal place.
return strconv.FormatFloat(n, 'f', 1, 64)
}

if *num < 1000 {
return strconv.FormatFloat(*num, 'f', 1, 64)
return formatNumber(*num)
}

if *num < 1000000 {
return strconv.FormatFloat(*num/1000, 'f', 1, 64) + "k"
return formatNumber(*num/1000) + "k"
}

return strconv.FormatFloat(*num/1000000, 'f', 1, 64) + "m"
return formatNumber(*num/1000000) + "m"
}

0 comments on commit 4b83f6f

Please sign in to comment.