package strutil import ( "fmt" "math" ) var ( sizesIEC = []string{ "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB", } sizes = []string{ "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB", } ) func logn(n, b float64) float64 { return math.Log(n) / math.Log(b) } func humanateBytes(s uint64, base float64, sizes []string) string { if s < 10 { return fmt.Sprintf("%dB", s) } e := math.Floor(logn(float64(s), base)) suffix := sizes[int(e)] val := float64(s) / math.Pow(base, math.Floor(e)) f := "%.0f" if val < 10 { f = "%.1f" } return fmt.Sprintf(f+"%s", val, suffix) } // FileSize calculates the file size and generate user-friendly string. func FileSizeIEC(s uint64) string { return humanateBytes(s, 1024, sizesIEC) } // FileSize calculates the file size and generate user-friendly string. func FileSize(s uint64) string { return humanateBytes(s, 1000, sizes) }