godash/internal/system/storage.go

35 lines
619 B
Go
Raw Normal View History

2023-06-14 21:53:27 +02:00
package system
import "fmt"
const (
KiB uint64 = 1024
MiB = KiB * 1024
GiB = MiB * 1024
TiB = GiB * 1024
PiB = TiB * 1024
EiB = PiB * 1024
)
func amountString(size uint64) (uint64, string) {
switch {
case size < MiB:
return KiB, "KiB"
case size < GiB:
return MiB, "MiB"
case size < TiB:
return GiB, "GiB"
case size < PiB:
return TiB, "TiB"
case size < EiB:
return PiB, "PiB"
default:
return EiB, "EiB"
}
}
func readableSize(size uint64) string {
unit, unitStr := amountString(size)
return fmt.Sprintf("%.2f %s", float64(size)/float64(unit), unitStr)
}