format.go 462 B

12345678910111213141516171819202122232425
  1. package format
  2. import (
  3. "fmt"
  4. "math"
  5. )
  6. const (
  7. Thousand = 1000
  8. Million = Thousand * 1000
  9. Billion = Million * 1000
  10. )
  11. func HumanNumber(b uint64) string {
  12. switch {
  13. case b > Billion:
  14. return fmt.Sprintf("%.0fB", math.Round(float64(b)/Billion))
  15. case b > Million:
  16. return fmt.Sprintf("%.0fM", math.Round(float64(b)/Million))
  17. case b > Thousand:
  18. return fmt.Sprintf("%.0fK", math.Round(float64(b)/Thousand))
  19. default:
  20. return fmt.Sprintf("%d", b)
  21. }
  22. }