mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2025-10-13 11:39:13 +00:00
18 lines
411 B
Go
18 lines
411 B
Go
package common
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
// FormatTraffic formats traffic bytes into human-readable units (B, KB, MB, GB, TB, PB).
|
|
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])
|
|
}
|