mirror of
https://github.com/will-moss/isaiah.git
synced 2024-08-22 23:27:15 +03:00
31 lines
575 B
Go
31 lines
575 B
Go
package ui
|
|
|
|
import "fmt"
|
|
|
|
func ByteCount(b int64) string {
|
|
const unit = 1000
|
|
if b < unit {
|
|
return fmt.Sprintf("%d B", b)
|
|
}
|
|
div, exp := int64(unit), 0
|
|
for n := b / unit; n >= unit; n /= unit {
|
|
div *= unit
|
|
exp++
|
|
}
|
|
return fmt.Sprintf("%.2f%cB",
|
|
float64(b)/float64(div), "kMGTPE"[exp])
|
|
}
|
|
func UByteCount(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("%.2f%cB",
|
|
float64(b)/float64(div), "kMGTPE"[exp])
|
|
}
|