Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/MHSanaei/3x-ui.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMHSanaei <mc.sanaei@gmail.com>2023-02-09 22:18:06 +0300
committerMHSanaei <mc.sanaei@gmail.com>2023-02-09 22:18:06 +0300
commitb73e4173a3c1e69e02ad6b4e3b43e425e57a5be9 (patch)
treed95d2f5e903d97082e11eb9f9023c165b1bde388 /util/common/format.go
3x-ui
Diffstat (limited to 'util/common/format.go')
-rw-r--r--util/common/format.go21
1 files changed, 21 insertions, 0 deletions
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))
+ }
+}