Skip to content

Commit

Permalink
refactor(utils): simplify format file size function
Browse files Browse the repository at this point in the history
  • Loading branch information
steveiliop56 committed Sep 22, 2024
1 parent 1b19dbd commit 023c247
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}
return fmt.Sprintf("%.2f%s", fileSize, sizes[i])
}

0 comments on commit 023c247

Please sign in to comment.