|
| 1 | +// Package bytesutil helps you convert byte sizes (such as file size, data uploaded/downloaded etc.) to |
| 2 | +// human-readable strings. This allows conversion to decimal and binary formats. |
| 3 | +// |
| 4 | +// See: https://en.m.wikipedia.org/wiki/Byte#Multiple-byte_units |
| 5 | +package bytesutil |
| 6 | + |
| 7 | +import "fmt" |
| 8 | + |
| 9 | +// Constants for byte sizes in decimal and binary formats |
| 10 | +const ( |
| 11 | + KILO int64 = 1000 // 1000 power 1 (10 power 3) |
| 12 | + KIBI int64 = 1024 // 1024 power 1 (2 power 10) |
| 13 | + MEGA = KILO * KILO // 1000 power 2 (10 power 6) |
| 14 | + MEBI = KIBI * KIBI // 1024 power 2 (2 power 20) |
| 15 | + GIGA = MEGA * KILO // 1000 power 3 (10 power 9) |
| 16 | + GIBI = MEBI * KIBI // 1024 power 3 (2 power 30) |
| 17 | + TERA = GIGA * KILO // 1000 power 4 (10 power 12) |
| 18 | + TEBI = GIBI * KIBI // 1024 power 4 (2 power 40) |
| 19 | + PETA = TERA * KILO // 1000 power 5 (10 power 15) |
| 20 | + PEBI = TEBI * KIBI // 1024 power 5 (2 power 50) |
| 21 | + EXA = PETA * KILO // 1000 power 6 (10 power 18) |
| 22 | + EXBI = PEBI * KIBI // 1024 power 6 (2 power 60) |
| 23 | +) |
| 24 | + |
| 25 | +// BinaryFormat formats a byte size to a human readable string in binary format. |
| 26 | +// Uses binary prefixes. See: https://en.m.wikipedia.org/wiki/Binary_prefix |
| 27 | +// |
| 28 | +// For example, |
| 29 | +// fmt.Println(bytesutil.BinaryFormat(2140)) |
| 30 | +// prints |
| 31 | +// 2.09 KiB |
| 32 | +func BinaryFormat(size int64) string { |
| 33 | + if size < 0 { |
| 34 | + return "" |
| 35 | + } else if size < KIBI { |
| 36 | + return fmt.Sprintf("%d B", size) |
| 37 | + } else if size < MEBI { |
| 38 | + return fmt.Sprintf("%.2f KiB", float64(size)/float64(KIBI)) |
| 39 | + } else if size < GIBI { |
| 40 | + return fmt.Sprintf("%.2f MiB", float64(size)/float64(MEBI)) |
| 41 | + } else if size < TEBI { |
| 42 | + return fmt.Sprintf("%.2f GiB", float64(size)/float64(GIBI)) |
| 43 | + } else if size < PEBI { |
| 44 | + return fmt.Sprintf("%.2f TiB", float64(size)/float64(TEBI)) |
| 45 | + } else if size < EXBI { |
| 46 | + return fmt.Sprintf("%.2f PiB", float64(size)/float64(PEBI)) |
| 47 | + } else { |
| 48 | + return fmt.Sprintf("%.2f EiB", float64(size)/float64(EXBI)) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// DecimalFormat formats a byte size to a human readable string in decimal format. |
| 53 | +// Uses metric prefixes. See: https://en.m.wikipedia.org/wiki/Metric_prefix |
| 54 | +// |
| 55 | +// For example, |
| 56 | +// fmt.Println(bytesutil.DecimalFormat(2140)) |
| 57 | +// prints |
| 58 | +// 2.14KB |
| 59 | +func DecimalFormat(size int64) string { |
| 60 | + if size < 0 { |
| 61 | + return "" |
| 62 | + } else if size < KILO { |
| 63 | + return fmt.Sprintf("%d B", size) |
| 64 | + } else if size < MEGA { |
| 65 | + return fmt.Sprintf("%.2f KB", float64(size)/float64(KILO)) |
| 66 | + } else if size < GIGA { |
| 67 | + return fmt.Sprintf("%.2f MB", float64(size)/float64(MEGA)) |
| 68 | + } else if size < TERA { |
| 69 | + return fmt.Sprintf("%.2f GB", float64(size)/float64(GIGA)) |
| 70 | + } else if size < PETA { |
| 71 | + return fmt.Sprintf("%.2f TB", float64(size)/float64(TERA)) |
| 72 | + } else if size < EXA { |
| 73 | + return fmt.Sprintf("%.2f PB", float64(size)/float64(PETA)) |
| 74 | + } else { |
| 75 | + return fmt.Sprintf("%.2f EB", float64(size)/float64(EXA)) |
| 76 | + } |
| 77 | +} |
0 commit comments