1
0
Fork 0

Updated ConvertFileSize(). Added TiB suffix.

This commit is contained in:
Alexander Andreev 2022-02-07 04:48:33 +04:00
parent db361bb509
commit 9955e3cd28
Signed by: Arav
GPG Key ID: 1327FE8A374CC86F
1 changed files with 15 additions and 5 deletions

View File

@ -1,16 +1,26 @@
package utils
import "fmt"
import (
"fmt"
"strconv"
"strings"
)
var sizeSuffixes = [...]string{"B", "KiB", "MiB", "GiB"}
var sizeSuffixes = [...]string{"B", "KiB", "MiB", "GiB", "TiB"}
// ConvertFileSize converts size in bytes down to biggest units it represents.
// Returns converted size, unit and, a concatenation of size and unit
func ConvertFileSize(size int64) (int64, string, string) {
func ConvertFileSize(size int64) (float64, string, string) {
var idx int
var fSize float64 = float64(size)
for idx = 0; size >= 1024; size /= 1024 {
for idx = 0; fSize >= 1024; fSize /= 1024 {
idx++
}
return size, sizeSuffixes[idx], fmt.Sprint(size, sizeSuffixes[idx])
fSizeStr := strconv.FormatFloat(fSize, 'f', 3, 64)
fSizeStr = strings.TrimRight(fSizeStr, "0")
fSizeStr = strings.TrimSuffix(fSizeStr, ".")
return fSize, sizeSuffixes[idx], fmt.Sprint(fSizeStr, sizeSuffixes[idx])
}