You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
24 lines
541 B
Go
24 lines
541 B
Go
1 year ago
|
package ngo
|
||
|
|
||
|
import "fmt"
|
||
|
|
||
|
// DataSize format bytes number friendly. eg: 1024 => 1KB, 1024*1024 => 1MB
|
||
|
//
|
||
|
// Usage:
|
||
|
//
|
||
|
// file, err := os.Open(path)
|
||
|
// fl, err := file.Stat()
|
||
|
// fmtSize := DataSize(fl.Size())
|
||
|
func DataSize(size uint64) string {
|
||
|
switch {
|
||
|
case size < 1024:
|
||
|
return fmt.Sprintf("%dB", size)
|
||
|
case size < 1024*1024:
|
||
|
return fmt.Sprintf("%.2fK", float64(size)/1024)
|
||
|
case size < 1024*1024*1024:
|
||
|
return fmt.Sprintf("%.2fM", float64(size)/1024/1024)
|
||
|
default:
|
||
|
return fmt.Sprintf("%.2fG", float64(size)/1024/1024/1024)
|
||
|
}
|
||
|
}
|