diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 62da61f..17188db 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -2,20 +2,14 @@ package utils import ( "fmt" - "strconv" ) func FormatFileSize(fileSize float64) (string) { - if fileSize < (1<<10) { - return fmt.Sprintf("%sB", strconv.Itoa(int(fileSize))) - } else if fileSize > (1<<10) && fileSize < (1<<20) { - return fmt.Sprintf("%.2fKB", fileSize/(1<<10)) - } else if fileSize > (1<<20) && fileSize < (1<<30) { - return fmt.Sprintf("%2.fMB", fileSize/(1<<20)) - } else if fileSize > (1<<30) && fileSize < (1<<40) { - return fmt.Sprintf("%.2fGB", fileSize/(1<<30)) - } else if fileSize > (1<<40) { - return fmt.Sprintf("%.2fTB", fileSize/(1<<40)) + sizes := []string{"B", "KB", "MB", "GB", "TB"} + i := 0 + for fileSize >= 1024 && i < len(sizes)-1 { + fileSize /= 1024 + i++ } - return fmt.Sprintf("%sB", strconv.Itoa(int(fileSize))) -} \ No newline at end of file + return fmt.Sprintf("%.2f%s", fileSize, sizes[i]) +}