17 lines
311 B
Go
17 lines
311 B
Go
|
package langext
|
||
|
|
||
|
import "fmt"
|
||
|
|
||
|
func FormatBytesToSI(b uint64) string {
|
||
|
const unit = 1000
|
||
|
if b < unit {
|
||
|
return fmt.Sprintf("%d B", b)
|
||
|
}
|
||
|
div, exp := uint64(unit), 0
|
||
|
for n := b / unit; n >= unit; n /= unit {
|
||
|
div *= unit
|
||
|
exp++
|
||
|
}
|
||
|
return fmt.Sprintf("%.1f %cB", float64(b)/float64(div), "kMGTPE"[exp])
|
||
|
}
|