2020-10-26 21:14:26 +08:00
|
|
|
package numberutils
|
|
|
|
|
|
2021-01-19 16:14:09 +08:00
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"strconv"
|
|
|
|
|
)
|
2020-10-26 21:14:26 +08:00
|
|
|
|
|
|
|
|
func FormatInt64(value int64) string {
|
|
|
|
|
return strconv.FormatInt(value, 10)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func FormatInt(value int) string {
|
|
|
|
|
return strconv.Itoa(value)
|
|
|
|
|
}
|
2021-01-19 16:14:09 +08:00
|
|
|
|
|
|
|
|
func FormatBytes(bytes int64) string {
|
|
|
|
|
if bytes < 1024 {
|
|
|
|
|
return FormatInt64(bytes) + "B"
|
|
|
|
|
} else if bytes < 1024*1024 {
|
2021-01-21 18:55:53 +08:00
|
|
|
return fmt.Sprintf("%.2fKB", float64(bytes)/1024)
|
2021-01-19 16:14:09 +08:00
|
|
|
} else if bytes < 1024*1024*1024 {
|
2021-01-21 18:55:53 +08:00
|
|
|
return fmt.Sprintf("%.2fMB", float64(bytes)/1024/1024)
|
2021-01-19 16:14:09 +08:00
|
|
|
} else if bytes < 1024*1024*1024*1024 {
|
2021-01-21 18:55:53 +08:00
|
|
|
return fmt.Sprintf("%.2fGB", float64(bytes)/1024/1024/1024)
|
2021-04-29 15:04:42 +08:00
|
|
|
} else if bytes < 1024*1024*1024*1024*1024 {
|
|
|
|
|
return fmt.Sprintf("%.2fTB", float64(bytes)/1024/1024/1024/1024)
|
|
|
|
|
} else if bytes < 1024*1024*1024*1024*1024*1024 {
|
|
|
|
|
return fmt.Sprintf("%.2fPB", float64(bytes)/1024/1024/1024/1024/1024)
|
2021-01-19 16:14:09 +08:00
|
|
|
} else {
|
2021-04-29 15:04:42 +08:00
|
|
|
return fmt.Sprintf("%.2fEB", float64(bytes)/1024/1024/1024/1024/1024/1024)
|
2021-01-21 18:55:53 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func FormatBits(bits int64) string {
|
|
|
|
|
if bits < 1000 {
|
|
|
|
|
return FormatInt64(bits) + "B"
|
|
|
|
|
} else if bits < 1000*1000 {
|
|
|
|
|
return fmt.Sprintf("%.2fKB", float64(bits)/1000)
|
|
|
|
|
} else if bits < 1000*1000*1000 {
|
|
|
|
|
return fmt.Sprintf("%.2fMB", float64(bits)/1000/1000)
|
|
|
|
|
} else if bits < 1000*1000*1000*1000 {
|
2021-02-07 09:16:58 +08:00
|
|
|
return fmt.Sprintf("%.2fGB", float64(bits)/1000/1000/1000)
|
2021-04-29 15:04:42 +08:00
|
|
|
} else if bits < 1000*1000*1000*1000*1000 {
|
|
|
|
|
return fmt.Sprintf("%.2fTB", float64(bits)/1000/1000/1000/1000)
|
|
|
|
|
} else if bits < 1000*1000*1000*1000*1000*1000 {
|
|
|
|
|
return fmt.Sprintf("%.2fPB", float64(bits)/1000/1000/1000/1000/1000)
|
2021-01-21 18:55:53 +08:00
|
|
|
} else {
|
2021-04-29 15:04:42 +08:00
|
|
|
return fmt.Sprintf("%.2fEB", float64(bits)/1000/1000/1000/1000/1000/1000)
|
2021-01-19 16:14:09 +08:00
|
|
|
}
|
|
|
|
|
}
|