31 lines
595 B
Go
31 lines
595 B
Go
|
/*
|
|||
|
* SPDX-FileCopyrightText: 2012–2015 Dustin Sallings <dustin@spy.net>
|
|||
|
* SPDX-License-Identifier: MIT
|
|||
|
*/
|
|||
|
|
|||
|
package template
|
|||
|
|
|||
|
import (
|
|||
|
"fmt"
|
|||
|
"math"
|
|||
|
)
|
|||
|
|
|||
|
func humanizeBytes(s int64) string {
|
|||
|
if s < 10 {
|
|||
|
return fmt.Sprintf("%d B", s)
|
|||
|
}
|
|||
|
base := 1024.0
|
|||
|
e := math.Floor(logn(float64(s), base))
|
|||
|
val := math.Floor(float64(s)/math.Pow(base, e)*10+0.5) / 10
|
|||
|
f := "%.0f %s"
|
|||
|
if val < 10 {
|
|||
|
f = "%.1f %s"
|
|||
|
}
|
|||
|
sizes := []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"}
|
|||
|
return fmt.Sprintf(f, val, sizes[int(e)])
|
|||
|
}
|
|||
|
|
|||
|
func logn(n, b float64) float64 {
|
|||
|
return math.Log(n) / math.Log(b)
|
|||
|
}
|