Files
3x-ui/internal/util/common/format.go
T
Javier Guerrero c321933a54 perf: reduce traffic formatter allocations (#6790)
Co-authored-by: Javier Guerrero <javier.guerrero@github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
2026-10-12 11:05:40 +02:00

17 lines
421 B
Go

package common
import "strconv"
// 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 strconv.FormatFloat(size, 'f', 2, 64) + units[unitIndex]
}