2023-01-15 14:08:45 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"git.milar.in/milarin/gmath"
|
|
|
|
)
|
|
|
|
|
|
|
|
func FormatBytes[T gmath.Integer](bytes T) string {
|
|
|
|
value := float64(bytes)
|
2023-01-15 14:25:37 +01:00
|
|
|
|
|
|
|
if value >= 1000000000000 {
|
2023-01-15 14:15:00 +01:00
|
|
|
return fmt.Sprintf("%.02fT", value/1000000000000)
|
2023-01-15 14:25:37 +01:00
|
|
|
} else if value >= 1000000000 {
|
2023-01-15 14:15:00 +01:00
|
|
|
return fmt.Sprintf("%.02fG", value/1000000000)
|
2023-01-15 14:25:37 +01:00
|
|
|
} else if value >= 1000000 {
|
2023-01-15 14:15:00 +01:00
|
|
|
return fmt.Sprintf("%.02fM", value/1000000)
|
2023-01-15 14:25:37 +01:00
|
|
|
} else if value >= 1000 {
|
2023-01-15 14:15:00 +01:00
|
|
|
return fmt.Sprintf("%.02fK", value/1000)
|
2023-01-15 14:08:45 +01:00
|
|
|
} else {
|
2023-01-15 14:15:00 +01:00
|
|
|
return fmt.Sprintf("%.02fB", value)
|
2023-01-15 14:08:45 +01:00
|
|
|
}
|
|
|
|
}
|