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

format.go « common « util - github.com/MHSanaei/3x-ui.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c73e3a016613409cbc7db3f9c5fd5eba61a64e40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package common

import (
	"fmt"
)

func FormatTraffic(trafficBytes int64) string {
	units := []string{"B", "KB", "MB", "GB", "TB", "PB"}
	unitIndex := 0
	size := float64(trafficBytes)

	for size >= 1024 && unitIndex < len(units)-1 {
		size /= 1024
		unitIndex++
	}
	return fmt.Sprintf("%.2f%s", size, units[unitIndex])
}