1
0

format.go 795 B

123456789101112131415161718192021
  1. package common
  2. import (
  3. "fmt"
  4. )
  5. func FormatTraffic(trafficBytes int64) (size string) {
  6. if trafficBytes < 1024 {
  7. return fmt.Sprintf("%.2fB", float64(trafficBytes)/float64(1))
  8. } else if trafficBytes < (1024 * 1024) {
  9. return fmt.Sprintf("%.2fKB", float64(trafficBytes)/float64(1024))
  10. } else if trafficBytes < (1024 * 1024 * 1024) {
  11. return fmt.Sprintf("%.2fMB", float64(trafficBytes)/float64(1024*1024))
  12. } else if trafficBytes < (1024 * 1024 * 1024 * 1024) {
  13. return fmt.Sprintf("%.2fGB", float64(trafficBytes)/float64(1024*1024*1024))
  14. } else if trafficBytes < (1024 * 1024 * 1024 * 1024 * 1024) {
  15. return fmt.Sprintf("%.2fTB", float64(trafficBytes)/float64(1024*1024*1024*1024))
  16. } else {
  17. return fmt.Sprintf("%.2fEB", float64(trafficBytes)/float64(1024*1024*1024*1024*1024))
  18. }
  19. }