Files
isaiah/app/server/ui/size.go
2024-01-04 01:24:18 +01:00

31 lines
575 B
Go

package ui
import "fmt"
func ByteCount(b int64) string {
const unit = 1000
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.2f%cB",
float64(b)/float64(div), "kMGTPE"[exp])
}
func UByteCount(b uint64) string {
const unit = 1000
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := uint64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.2f%cB",
float64(b)/float64(div), "kMGTPE"[exp])
}