infra
/
goutils
Archived
1
0
Fork 0

용량 계산 로직 수정

This commit is contained in:
Sangbum Kim 2018-07-06 14:19:56 +09:00
parent f673c30fb4
commit c9fbc24e51
1 changed files with 29 additions and 25 deletions

View File

@ -127,29 +127,30 @@ func FormatInt32(value int32) string { return strconv.FormatInt(int64(value), 10
func FormatInt64(value int64) string { return strconv.FormatInt(value, 10) } func FormatInt64(value int64) string { return strconv.FormatInt(value, 10) }
func FormatInt(value int) string { return strconv.FormatInt(int64(value), 10) } func FormatInt(value int) string { return strconv.FormatInt(int64(value), 10) }
const ( var (
Byte = 1 << (10 * iota) sizesIEC = []string{
KiByte "B",
MiByte "KiB",
GiByte "MiB",
TiByte "GiB",
PiByte "TiB",
EiByte "PiB",
ZiByte "EiB",
YiByte "ZiB",
) "YiB",
var bytesSizeTable = map[string]uint64{
"B": Byte,
"KiB": KiByte,
"MiB": MiByte,
"GiB": GiByte,
"TiB": TiByte,
"PiB": PiByte,
"EiB": EiByte,
"ZiB": ZiByte,
"YiB": YiByte,
} }
sizes = []string{
"B",
"KB",
"MB",
"GB",
"TB",
"PB",
"EB",
"ZB",
"YB",
}
)
func logn(n, b float64) float64 { func logn(n, b float64) float64 {
return math.Log(n) / math.Log(b) return math.Log(n) / math.Log(b)
@ -171,11 +172,14 @@ func humanateBytes(s uint64, base float64, sizes []string) string {
} }
// FileSize calculates the file size and generate user-friendly string. // FileSize calculates the file size and generate user-friendly string.
func FileSize(s uint64) string { func FileSizeIEC(s uint64) string {
sizes := []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"} return humanateBytes(s, 1024, sizesIEC)
return humanateBytes(s, 1024, sizes)
} }
// FileSize calculates the file size and generate user-friendly string.
func FileSize(s uint64) string {
return humanateBytes(s, 1000, sizes)
}
func AspectRatio(srcRect image.Point, toResize uint64) image.Point { func AspectRatio(srcRect image.Point, toResize uint64) image.Point {
w, h := int(toResize), getRatioSize(int(toResize), srcRect.Y, srcRect.X) w, h := int(toResize), getRatioSize(int(toResize), srcRect.Y, srcRect.X)
if srcRect.X < srcRect.Y { if srcRect.X < srcRect.Y {