2023-11-09 01:55:46 +00:00
|
|
|
package format
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
2024-08-01 21:52:15 +00:00
|
|
|
"strconv"
|
2023-11-09 01:55:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
Thousand = 1000
|
|
|
|
Million = Thousand * 1000
|
|
|
|
Billion = Million * 1000
|
|
|
|
)
|
|
|
|
|
|
|
|
func HumanNumber(b uint64) string {
|
|
|
|
switch {
|
2024-05-07 21:41:53 +00:00
|
|
|
case b >= Billion:
|
|
|
|
number := float64(b) / Billion
|
|
|
|
if number == math.Floor(number) {
|
|
|
|
return fmt.Sprintf("%.0fB", number) // no decimals if whole number
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%.1fB", number) // one decimal if not a whole number
|
|
|
|
case b >= Million:
|
|
|
|
number := float64(b) / Million
|
|
|
|
if number == math.Floor(number) {
|
|
|
|
return fmt.Sprintf("%.0fM", number) // no decimals if whole number
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%.2fM", number) // two decimals if not a whole number
|
|
|
|
case b >= Thousand:
|
|
|
|
return fmt.Sprintf("%.0fK", float64(b)/Thousand)
|
2023-11-09 01:55:46 +00:00
|
|
|
default:
|
2024-08-01 21:52:15 +00:00
|
|
|
return strconv.FormatUint(b, 10)
|
2023-11-09 01:55:46 +00:00
|
|
|
}
|
|
|
|
}
|