From b73e4173a3c1e69e02ad6b4e3b43e425e57a5be9 Mon Sep 17 00:00:00 2001 From: MHSanaei Date: Thu, 9 Feb 2023 22:48:06 +0330 Subject: 3x-ui --- util/common/format.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 util/common/format.go (limited to 'util/common/format.go') diff --git a/util/common/format.go b/util/common/format.go new file mode 100644 index 00000000..1ea10877 --- /dev/null +++ b/util/common/format.go @@ -0,0 +1,21 @@ +package common + +import ( + "fmt" +) + +func FormatTraffic(trafficBytes int64) (size string) { + if trafficBytes < 1024 { + return fmt.Sprintf("%.2fB", float64(trafficBytes)/float64(1)) + } else if trafficBytes < (1024 * 1024) { + return fmt.Sprintf("%.2fKB", float64(trafficBytes)/float64(1024)) + } else if trafficBytes < (1024 * 1024 * 1024) { + return fmt.Sprintf("%.2fMB", float64(trafficBytes)/float64(1024*1024)) + } else if trafficBytes < (1024 * 1024 * 1024 * 1024) { + return fmt.Sprintf("%.2fGB", float64(trafficBytes)/float64(1024*1024*1024)) + } else if trafficBytes < (1024 * 1024 * 1024 * 1024 * 1024) { + return fmt.Sprintf("%.2fTB", float64(trafficBytes)/float64(1024*1024*1024*1024)) + } else { + return fmt.Sprintf("%.2fEB", float64(trafficBytes)/float64(1024*1024*1024*1024*1024)) + } +} -- cgit v1.2.3